Operators
Operators
Module-2 OPERATORS:
1. Arithmetic Operators
The following table shows all the arithmetic operators supported by the C
language. Assume variable A holds 10 and variable B holds 20 then −
Example:
i. 5+3=8
ii. 5–3=2
iii. 5*3=15
iv. 5/3=1
v. 5%3=2
2.Relational Operators
The following table shows all the relational operators supported
by C. Assume variable A holds 10 and variable B holds 20 then
−
== Checks if the values of two operands are equal or not. If yes, then (A == B)
the condition becomes true. is not
true.
!= Checks if the values of two operands are equal or not. If the values (A != B)
are not equal, then the condition becomes true. is true.
3
> Checks if the value of left operand is greater than the value of right (A > B)
operand. If yes, then the condition becomes true. is not
true.
< Checks if the value of left operand is less than the value of right (A < B)
operand. If yes, then the condition becomes true. is true.
>= Checks if the value of left operand is greater than or equal to the (A >= B)
value of right operand. If yes, then the condition becomes true. is not
true.
<= Checks if the value of left operand is less than or equal to the value (A <= B)
of right operand. If yes, then the condition becomes true. is true.
Example:
Let x=2 and y=5 then
i. x < y = True
ii. (x + 2) > (y * 2)= False
iii. (x + 3) <= y = True
iv. x != y = True
v. y > (3 + x)= False
3.Logical Operators
An operator that compare or evaluate logical and relational expressions .
Operator Name
&& Logical AND
|| Logical OR
! Logical NOT
4
Example
Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, (A && B)
then the condition becomes true. is false.
! Called Logical NOT Operator. It is used to reverse the logical state !(A &&
of its operand. If a condition is true, then Logical NOT operator will B) is
make it false. true.
Logical AND
Exp1 Exp2 Exp1 &&
Exp2
False False False
True False False
False True False
True True True
Logical OR
Logical NOT
Example:
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
& Binary AND Operator copies a bit to the result if it exists in both (A & B) = 12,
operands. i.e., 0000
1100
^ Binary XOR Operator copies the bit if it is set in one operand but (A ^ B) = 49,
not both. i.e., 0011
0001
7
~ Binary One's Complement Operator is unary and has the effect (~A ) = ~(60),
of 'flipping' bits. i.e,. -0111101
<< Binary Left Shift Operator. The left operands value is moved left A << 2 = 240
by the number of bits specified by the right operand. i.e., 1111
0000
>> Binary Right Shift Operator. The left operands value is moved A >> 2 = 15
right by the number of bits specified by the right operand. i.e., 0000
1111
AMARJIT KUMAR