Unit 2.2 Operators
Unit 2.2 Operators
STRUCTURED DESIGN
Arithmetic * multiply
/ divide( divisor must be non zero )
Operators % modulo(gives remainder after
div)
The parenthesis() are used to clarify complex
operations. The operators + and - can be used as
unary plus and unary minus arithmetic operators
also. The unary – negates the sign of it’s operand .
Note : C language has no operator for
exponentiation.
Arithmetic (x-y)*(x+y)/5
Here x,y and 5 are operands and the symbols -,*,+,/ are
Expressions operators.
The precedence of operators for the expression
evaluation has been given by using parenthesis
which will over rule the operators precedence. If
x=25 and y=15,then the value of this expression will
be 80.
Consider the following expression :
3*((a%4)*(5+(b-2)/(c+3)))
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;
Decrement a = 10;
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 :
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.
among
2.Unary ! NOT
~ Bitwise(1’s) component
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
and across
>= Greater than equal to
8.Equality == Equal to
all the sets
!= Not equal to of
9.Bitwise AND & Bitwise AND
operators.
10.Bitwise XOR ^ Bitwise XOR
11.Bitwise OR | Bitwise OR
Category Operator What it does ?
12.Logical AND && Logical AND
/=
Assign product
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
13.Logical OR
&&
||
Left to Right
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