Unit 4
Unit 4
15 * 7 / ( 2 – 3 * 5 / 7 + 4 ) – 7 * 9 % 4
Arithmetic = 15 * 7 / (2 – 15 / 7 + 4 ) – 7 * 9 % 4
Operators = 15 * 7 / (2 – 2 + 4) – 7 * 9 % 4
= 15 * 7 / 4 – 7 * 9 % 4
Precedence
= 105 / 4 - 63 % 4
= 26 – 3
= 23
C language has two useful operators called increment(++) and
decrement (--) that operate on integer data only.
The increment (++) operator increments the operand by 1, while
the decrement operator (--) decrements the operand by 1.
There are two type of increment (++) operator : pre and post
Increment ,Similarly There are two type of decrement (--) operator : pre
and post, for example :
and int a , b;
a = 10;
Decrement b = a++ ;
Operator printf(“ %d %d “, a, b);
OUTPUT
11 10 . First a is assigned to b and then a is
incremented by 1 i.e.,post-increment takes place
If we have : int a, b ;
a = 20;
b = ++a;
printf(“%d %d”, a, b);
Increment OUTPUT : 21 21. first a is incremented by 1 and then
assignment take place i.e., pre-increment of a.
and now, consider the example for (--) operator :
int a,b;
Decrement a=10;
Operator b= a--;
printf(“%d %d”, a , b)
OUTPUT : 9 10. first a is assigned to b then a is
decremented by 1. i.e.,post decrement takes place
If we have :
int i, j ;
I = 20;
j = --i;
printf(“%d %d”, i, j);
Decrement OUTPUT : 19 19. first i is decremented by 1 and
Operator then assignment take place i.e., pre-decrement of
i.
OPERATOR MEANING
== Equals
Relational != Not Equals
< Less than
Operators > Greater than
<= Less than or
equals
>= Greater than or
equals
In C, we can have simple conditions (single) or
compound conditions(two or more). The logical
operators are used to combine conditions. The
notations for these operators is given below :
Operator Notation in C
Logical NOT !
AND &&
Operators
OR ||
The notation for the operator OR is given by two
broken lines. These follow the same precedence
as in other language. NOT(!) is evaluated before
AND(&&) which is evaluatedbefore OR(||).
Parenthesis( ) cab be used to change this order.
&& : returns true when all the condition under the
consideration are true and returns false when
anyone or more than one condition is false.
|| : returns true when one or more than one
condition under the consideration are true
Logical and returns false when all the conditions are
Operators false
! : NOT operator is used to complement the
condition under the consideration
Returns true when condition is false and returns
false when condition is true
Precedence of Relational Operators and Logical
Operators
This operator ? And : together forms a ternary operator called as the conditional
operator.
sizeof(operand)
The sizeof
Here the operand is a built in or user defined data type or variable.
Operator The sizeof operator always precedes its operand.
For example, sizeof (float) returns the value 4 .
The sizeof operator mainly used in dynamic memory allocation
for calculating the number of bytes used by some user defined
data type.
Precedence of operators among themselves and
across all the sets of operators.
themselves +
-
Unary plus
Unary minus
and across all 3.Member acces .* Dereference
operators. /
%
Divide
Remainder (Modulus)
Category Operator What it does ?
5.Additive + Binary plus
- Binary minus Precedence
6.Shift << Shift left
of operators
>> Shift right
7.Relational < Less than
among
<= Less than or equal to themselves
>
>=
Greater than
Greater than equal to
and across
8.Equality == Equal to all the sets
!= Not equal to of
9.Bitwise AND
10.Bitwise XOR
&
^
Bitwise AND
Bitwise XOR
operators.
11.Bitwise OR | Bitwise OR
Category Operator What it does ?
12.Logical AND && Logical AND
/= Assign quotient
themselves
%= Assign remainder (modulus)
and across
+= Assign sum all the sets
-= Assign difference of
&= Assign bitwise AND operators.
^= Assign bitwise XOR
|= Assign bitwise OR
of the ~
+
Operator -
++
--
&
*
sizeof
Category Operator Associativity
3.Member access .* Left to Right
->*
4.Multiplication * Left to right
/
%
Associativity
5.Additive + Left to Right
- of the
6.Shift << Left to Right Operator
>>
7.Relational < Left to Right
<=
>
>=
Category Operator Associativity
8.Equality == Left to Right
!=
9.Bitwise AND & Left to right
10.Bitwise XOR ^ Left to Right
11.Bitwise OR | Left to Right Associativity
12.Logical AND && Left to Right
13.Logical OR || Left to Right
of the
14.Conditional ?: Right to Left Operator
15.Assignment = Right to Left
*=
/=
%=
+=
-=
Category Operator Associativity
&= Right to Left
^=
|=
<<=
>>=
16.Comma . Left to Right