0% found this document useful (0 votes)
16 views

Operators in C Programming

Operators in C programming

Uploaded by

roopesh1422
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Operators in C Programming

Operators in C programming

Uploaded by

roopesh1422
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

OPERATORS

OPERATOR

 An Operator is a symbol that specifies what


operation need to be performed
 Example:
 + for addition operation
OPERAND

 An Operand can be constant or a variable


EXPRESSION

 An expression is a combination of Operator


or Operands that reduces to a single value
 Example:
a+b
CLASSIFICATION

 The classification is based on type of operation


1. Arithmetic operator
2. Assignment operator
3. Relational operator
4. Logical operator
5. Increment/Decrement operator
6. Conditional operator
7. Bitwise operator
8. Special operator
ARITHMETIC OPERATORS

 These operators are used to perform basic


arithmetic operation
 There are 5 Arithmetic operators
ARITHMETIC OPERATORS

 + → used to perform addition operation


 - → used to perform subtraction operation
 * → used to perform multiplication operation
 / → used to perform division, returns quotient
 % → divides first operand by second operand,
returns remainder. Both operands must be
integer
ARITHMETIC 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

 These operators are used to find relationship


between two operands
 They are mainly used for comparison
 The output of relational expression is
true(1) or false(0)
 There are 6 relational operators
RELATIONAL OPERATOR
RELATIONAL OPERATOR

> → If 1st operand is greater than 2nd operand then it returns


true(1) else returns false(0)
< → If 1st operand is less than 2nd operand then it returns
true(1) else returns false(0)
>= → If 1st operand is greater than or equal to 2nd operand then
it returns true(1) else returns false(0)
<= → If 1st operand is less than or equal to 2nd operand then it
returns true(1) else returns false(0)
== → If both the operands are equal then it returns true(1) else
returns false(0)
!= → If both operands are not equal then it returns true(1) else
returns false(0)
RELATIONAL OPERATOR

 Greater than (>):


 Less than (<):
 Greater than or equal to(>=):
 Less than or equal to(<=):
 Equal to (==):
 Not Equal to (!=):
RELATIONAL OPERATORS

Condition Return Value


3>4 0
7 <= 15 1
5 != 4 1
1<9 1
10 >= 10 1
13 == 13 1
RELATIONAL OPERATORS
Priority Associative
> - 1 Left to Right
< - 1
>= - 1
<= - 1
== - 2
!= - 2
Evaluation of Expression

1. Evaluate expression within parenthesis


2. Evaluate unary operators
3. Evaluate arithmetic expression
4. Evaluate relation expression
RELATIONAL OPERATORS

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

 These operators are used to combine two or more


relational expression
 The output of logical expression is true(1) or false(0)
 All non-zero values are treated as true
 Zero value is treated as false
 These operators are used to perform logical
operation like conjunction, Disjunction, negation
 There are 3 logical operators
LOGICAL OPERATORS
LOGICAL NOT (!)

 If operand is true(1) then result of Logical Not


is false(0)
 If operand is false(0) then result of Logical Not
is true(1)
 Truth Table for Logical Not is

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

True(1) && True(1) True(1)

True(1) && False(0) False(0)

False(0) && True(1) False(0)

False(0) && False(0) False(0)


LOGICAL OR (||)
 Output of Logical OR is true(1) if any of the
operand is true
 Else output of Logical OR is false(0) if both
0perands are false
 Truth Table for Logical OR is
Operand1 || Operand2 Result

True(1) || True(1) True(1)

True(1) || False(0) True(1)

False(0) || True(1) True(1)

False(0) || False(0) False(0)


LOGICAL OPERATORS

 Logical AND
 Logical OR
 Logical NOT
LOGICAL OPERATORS

Condition Return Value


(3 > 4) && 6 F
5 < 15 || (2 >1) T
!(6 == 6) F
9 && 0 F
(12 < 3) || (6 >12) F
!(1) F
LOGICAL OPERATORS

Priority
! - 1 Associative
&& - 2 Left to Right
|| - 3

Evaluation of Expression

1. Evaluate expression within parenthesis


2. Evaluate unary operators
3. Evaluate arithmetic expression
4. Evaluate relation expression
5. Evaluate logical expression
LOGICAL OPERATORS
Evaluation
Of
Evaluates +
Expression

Evaluates -

Evaluates >

This condition
Evaluates &&
is false
LOGICAL OPERATORS
Evaluation
of Evaluates
Expression

Evaluates

Evaluates

This condition
is
ASSIGNMENT OPERATOR

 These operators are used to assign the data or


result of an expression into a variable
 The most common assignment operator is =
 There are 3 types of assignment operator
ASSIGNMENT OPERATORS
SIMPLE ASSIGNMENT OPERATOR

 Most commonly used Assignment operator


 This operator assigns the value in Right side
to Left side
 Like variable = value, variable = expression,
variable = variable

a=5 Sum = 5+3 a=5 5=a


b=a
SHORTHAND ASSIGNMENT OPERATOR

 The operators like +=, *= are called shorthand


assignment operators
 The expression a=a+10 can be written as a+=10

Operator Example Same as


+= a+=b a=a+b
a=5
-= a-=b a=a-b a+=10
*= a*=b a=a*b Now
/= a/=b a=a/b a=15
%= a%=b a=a%b
MULTIPLE ASSIGNMENT OPERATOR

 Whenever we want assign a single value to


multiple variables in a single line then we
can use the simple assignment operator like
this

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

 Increment/Decrement operator is a unary


operator
 Increment operator is used to increment the
value of a variable by one
 Decrement operator is used to decrement the
value of a variable by one
 There are two operators
INCREMENT/DECREMENT OPERATOR
INCREMENT 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

1. Evaluate expression within parenthesis


2. Evaluate unary operators (increment/decrement operator)
3. Evaluate arithmetic expression
4. Evaluate relation expression
5. Evaluate logical expression
6. Evaluate assignment operator
INCREMENT/DECREMENT OPERATORS
Evaluation a=5
b = 10
of Evaluates
Expression
b++
a++
Evaluates So the value is 11
So the value is 6

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

 These operators are used to manipulate the


bits of the given data
 These operators perform logical operation on
individual bits of binary number
 There are 6 bitwise operators
BITWISE OPERATORS
BITWISE OPERATOR

 Before knowing about Bitwise operator, first let us


see how to convert a given number to binary
32 16 8 4 2 1
5 4 3 2 1 0
2 2 2 2 2 2

 If we want to represent 5 in binary form with four


digits
8 4 2 1
3 2 1 0
2 2 2 2
0 1 0 1
BITWISE OPERATOR

 Another method to convert a given number to


binary
 If we want to represent 5 in binary form with
four digits

2 5
2 2 -- 1
1 -- 0
5 in Binary form 1 0 1
BITWISE AND

 If the corresponding bit position in both the


operands are 1 then the result is 1 otherwise 0
Operand1 & Operand2 Result
1 & 1 1
1 & 0 0
0 & 1 0
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 0 0 1 0 2
BITWISE OR

 If the corresponding bit position in both the


operands are 0 then the result is 0 otherwise 1
Operand1 | Operand2 Result
1 | 1 1
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 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

 The operator is <<


 This operator is used to shift the data by a specified
number of bit positions towards left
 Let us consider a << num,
a -- data to be shifted to left MSB are discarded
num – number of bit position
a=5 0 0 0 0 0 1 0 1
b = a << num
LSB is
b = 5 << 1 b= appended
0 0 0 0 1 0 1 0
5<<1 with zero’s
10
RIGHT SHIFT

 The operator is >>


 This operator is used to shift the data by a specified
number of bit positions towards right
 Let us consider a >> num,
a -- data to be shifted to right
num – number of bit position LSB are discarded

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

You might also like