Operators in C Programming
Operators in C Programming
OPERATOR
Addition (+):
Subtraction (-):
Multiplication (*):
Division (/):
Modulus (%):
ARITHMETIC OPERATORS
Priority
* - 1
Associative
/ - 1
Left to Right
% -1
+ -2
- - 2
Evaluation of Expression
Evaluates * Then
first Evaluates +
ARITHMETIC OPERATORS
Evaluation of Expression
Evaluates / Then
first Evaluates *
ARITHMETIC OPERATORS
Evaluation of Expression
Evaluates ( ) Then
first Evaluates *
ARITHMETIC OPERATORS
Evaluation of Expression
Evaluates
Evaluates
Evaluates
PROGRAM
RELATIONAL OPERATOR
Evaluation of Expression
Evaluates +
Then
first
Evaluates -
This condition
is true
RELATIONAL OPERATORS
Evaluation of Expression
Evaluates *
Evaluates +
Evaluates >
This condition
is false
RELATIONAL OPERATORS
Evaluation of Expression
Evaluates
Evaluates
Evaluates
This condition
is
PROGRAM
LOGICAL OPERATORS
Operand !Operand
True(1) False(0)
False(0) True(1)
–
LOGICAL AND(&&)
Output of Logical AND is true(1) if and only if
both operands are true
Else output of Logical AND is false(0) if any of
the operand is false
Truth Table for Logical AND is
Operand1 && Operand2 Result
Logical AND
Logical OR
Logical NOT
LOGICAL OPERATORS
Priority
! - 1 Associative
&& - 2 Left to Right
|| - 3
Evaluation of Expression
Evaluates -
Evaluates >
This condition
Evaluates &&
is false
LOGICAL OPERATORS
Evaluation
of Evaluates
Expression
Evaluates
Evaluates
This condition
is
ASSIGNMENT OPERATOR
a=b=c=5
ASSIGNMENT OPERATORS
Priority
Associative
= - 1
Right to Left
+= - 1
-= - 1
*= - 1
/= - 1 Evaluation of Expression
%= - 1
1. Evaluate expression within parenthesis
2. Evaluate unary operators
3. Evaluate arithmetic expression
4. Evaluate relation expression
5. Evaluate logical expression
6. Evaluate assignment operator
ASSIGNMENT OPERATORS
Evaluation
C -= 5
of Evaluates
C=C–5
Expression C=7–5
C=2
A=1 Evaluates
B *= 2
B=3
B=B*2
C=7
Evaluates B=3*2
B=6
A += 6
A=A+6
A=1+6
A=7
INCREMENT/DECREMENT OPERATOR
++ is an increment operator
This operator is used to increment the value
of variable by one
There are two categories of increment
operator
1. Post increment (ex: c++)
2. Pre increment (ex: ++c)
POST INCREMENT
In Post increment, ++ operator is placed immediately
after the operand
A++
As the name indicates,
❖ First use the value of operand/variable
❖ then increment the value of operand/variable by 1
a
5
Let a = 5
b
1 b = a++ 5
b=5 a
2
a=6 6
PRE INCREMENT
In Pre increment, ++ operator is placed before the
operand ++a
As the name indicates,
❖ First increment the value of operand/variable
by 1
❖ then use the incremented value of
a
operand/variable
Let a = 5 5
1 b = ++a a
6
2
a=6 b
b=6 6
DECREMENT OPERATOR
-- is an decrement operator
This operator is used to decrement the value
of variable by one
There are two categories of decrement
operator
1. Post decrement (ex: c--)
2. Pre decrement (ex: --c)
POST DECREMENT
In Post decrement, -- operator is placed
immediately after the operand A--
As the name indicates,
❖ Firstuse the value of operand/variable
❖ then decrement the value of
a
operand/variable by 1 5
Let a = 5
b
1
b = a-- 5
2
b=5 a
a=4 4
PRE DECREMENT
In Pre decrement, -- operator is placed before the
operand --A
As the name indicates,
❖ First decrement the value of operand/variable
by 1
❖ then use the decremented value of a
operand/variable 5
Let a = 5
1 b = --a a
4
2
a=4 b
b=4 4
INCREMENT/DECREMENT OPERATORS
Priority
Associative
++ - 1
Right to Left
-- - 1
Evaluation of Expression
Evaluates
INCREMENT/DECREMENT OPERATORS
a
Evaluation a=5
5 1
of Evaluates
a
Expression
a 6 5
2 ++a
a++ 3
5 Evaluates Increment the value of a
Use value of a 7
Then increment a
Then use
incremented value
a
a
6 6
7
4
INCREMENT/DECREMENT OPERATORS
Evaluation a=5
of Evaluates
Expression
Evaluates
a a a Evaluates
5 4 5
INCREMENT/DECREMENT OPERATORS
Evaluation x = 10
of Evaluates
Expression
Evaluates
x x
10 11
CONDITIONAL OPERATOR
Also called ternary operator
It operates on three operands
The operators are ? and :
Syntax:
(expr1) ? expr2 : expr3;
where expr1 is evaluated to true or false
if expr1 is true then expr2 is executed
if expr1 is false then expr3 is executed
CONDITIONAL OPERATOR
CONDITIONAL OPERATOR
Conditional Operator(?:)
Let a = 5, b=4
max = (a > b) ? a : b;
max = (5 > 4) ? 5 : 4;
So, max = 5
BITWISE OPERATOR
2 5
2 2 -- 1
1 -- 0
5 in Binary form 1 0 1
BITWISE AND
a=10 0 0 0 0 1 0 1 0
b=6 0 0 0 0 0 1 1 0
a&b 0 0 0 0 0 0 1 0 2
BITWISE OR
a=10 0 0 0 0 1 0 1 0
b=6 0 0 0 0 0 1 1 0
a|b 0 0 0 0 1 1 1 0 14
BITWISE XOR
If the corresponding bit position in both the
operands are different then the result is 1
otherwise 1
Operand1 ^ Operand2 Result
1 ^ 1 0
1 ^ 0 1
0 ^ 1 1
0 ^ 0 0
a=10 0 0 0 0 1 0 1 0
b=6 0 0 0 0 0 1 1 0
a^b 0 0 0 0 1 1 0 0 12
LEFT SHIFT
a=10 0 0 0 0 1 0 1 0
b = a >> num
b = 10 >> 1 b=
0 0 0 0 0 1 0 1
10<<1 MSB is
appended
5 with zero’s
BITWISE NEGATE
The operator is ~
This operator changes every bit of an operand
from 0 to 1 and 1 to 0
Operand ~Operand
1 0
0 1
a=10 0 0 0 0 1 0 1 0
b=~a 1 1 1 1 0 1 0 1 245
BITWISE OPERATORS
Priority
~ - 1 Associative
<< - 2 Left to Right
>> - 2
& - 3
Evaluation of Expression
^ - 4
| - 5
1. Evaluate expression within parenthesis
2. Evaluate unary operators (increment/decrement operator)
3. Evaluate arithmetic expression
4. Evaluate Left shift and Right shift
5. Evaluate relation expression
6. Evaluate Bitwise AND,XOR,OR
7. Evaluate logical expression
8. Evaluate assignment operator
BITWISE OPERATORS
Evaluates 0 0 0 0 1 0 1 0
10 << 2
0 0 0 1 0 1 0 0
Evaluates
5 0 0 0 0 0 1 0 1
20 0 0 0 1 0 1 0 0
5&20 0 0 0 0 0 1 0 0
Evaluation
SPECIAL OPERATOR
Some of the special operators that we dicuss are:
1. , (comma) – used to link related expression
ex: int x,y;
2. sizeof – it returns the number of bytes the
operand occupies
ex: sizeof(int);
3. Pointer operators (&, *)
4. Member selection operator (. , ->)
Thank You