02 Module2 Operators
02 Module2 Operators
Relational Operators: These are used to compare two quantities. The output will be either 0
(False) or 1(True). The different relational operators are:
Operator Name Syntax Example
(b=5, c=2)
< Lesser than b<c 0 (False)
> Greater than b>c 1 (True)
<= Lesser than or Equal to b <= c 0 (False)
>= Greater than or Equal to b >= c 1 (True)
Equality Operators:
1
Operator Name Syntax Example (b=5, c=2)
== Equal to b == c a = 0 (False)
!= Not equal to b!= c a = 1 (True)
Logical Operators: C language supports three logical operators- logical AND (&&), logical
OR (||), and logical NOT (!). These are used to test more than one condition and make
decision.
Logical AND (&&) The output is true only if both inputs are true. It accepts two or more
inputs.
Input Output
X Y X && Y
0 0 0
0 1 0
1 0 0
1 1 1
Logical OR (||) The output is true only if any of its input is true. It accepts two or more
inputs.
Input Output
X Y X || Y
0 0 0
0 1 1
1 0 1
1 1 1
Logical NOT (!) The output is true when input is false and vice versa. It accepts only
one input.
Input Output
X !X
0 1
1 0
2
Unary Operators: Unary operator acts on single operands. C language supports three
unary operators-unary minus, increment and decrement operators.
When an operand is preceded by a minus sign, the unary operator negates its value.
For example:
int a, b = 10;
a= -(b)
then result of the expression is a=-10
For example:
int x = 10, y;
y = x++;
is equivalent to writing
y=x;
x=x+1;
whereas,
y = ++x;
is equivalent to writing
x=x+1;
y=x;
3
exp1 is evaluated first. If it is true, then exp2 is evaluated and becomes the result of the
expression, otherwise exp3 is evaluated and becomes the result of the expression.
For example,
large = (a> b)? a: b
First exp1, that is (a > b) is evaluated. If a is greater than b, then large = a, else large = b.
Hence, large is equal to either a or b but not both
Bitwise Operators:
Bitwise operators are those operators that perform operations at bit level. These operators
include: bitwise AND, bitwise OR, bitwise XOR, and shift operators.
Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^)
X Y X&Y X|Y X^Y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Shift Operators
C supports two bitwise shift operators. They are shift-left (<<) and shift right (>>). The syntax
for a shift operation can be given as:
operand op num
where the bits in operand are shifted left or right depending on the operator.
For example, if x = 0001 1101, then x << 1 produces 0011 1010
When operator is shift left, then MSB of x will be lost and LSB are set to 0.
For example, if x = 0001 1101, then x >>1 produces 0000 1110
When operator is shift right, then LSB of x will be lost and MSB are set to 0.
Assignment Operators:
These are used to assign the result or values to a variable. The different types of assignment
operators are:
4
Simple Assignment a = 10
Shorthand Assignment a += 10 equal to a = a + 10
Multiple Assignment a = b = c = 10
Sizeof Operator: It is used to determine the size of variable or value in bytes. For ex:
int a = 10;
unsigned int result;
result = sizeof(a);
Output:
20 16 36 9 0
0110
01
011
34
17 16
4
0 20 20 -17 32 8
9
1
5
Evaluate the following expressions step by step
1. A=x*2+y/5-z*y where x=3, y=5, z=7
A=x*2+y/5-z*y
=3*2+5/5-7*5
=6+5/5-7*5
=6+1-7*5
=6+1-35
=7-35
=-31
2. A=++x*(y-3)/2-z++*y where x=3, y=5, z=7
A=++x*(y-3)/2-z++*y
=++x*(5-3)/2-z++*5
=++x*2/2-7*5
=4*2/2-7*5
=8/2-7*5
=4-7*5
=4-35
=-31
6
+(unary)
-(unary)
!
~
(type)
*(indirec
tion)
&(addres
s)
sizeof
*/ % left to right && left to right
+ - left to right || left to right
<< >> left to right ?: right to left
< <= left to right = right to left
> >= += -=
*= /=
%= &=
^= |=
<<=
>>=
== != left to right (comma) left to right
7
Typecasting: It is also known as forced conversion. Typecasting an arithmetic
expression tells the compiler to represent the value of the expression in a certain way.
For ex:
float salary = 10000.00;
int sal;
sal =(int) salary;
In this example floating point numbers are converted to integer by typecasting.