Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

CHAPTER 4

OPERATORS &
EXPRESSIONS
3 Hours
~5 marks
OPERATORS ,OPERANDS AND EXPRESSIONS
Operator:
 A symbol that operates on single or multiple data items is called an operator.
 Used in program to perform arithmetic or logical operations or
manipulations.

Operands:
 Data items that operators act upon are called operands.

Example:
In an expression (x + b) , here ‘+’ symbol is an operator that adds two
operands ‘x’ and ‘b’.
2
Expression:
 Combination of variables ,constants and operators written according to syntax
of the language representing some meaningful information.
 Example are:
8+9 , a+b-c, x*y/3 , etc.

3
TYPES OF OPERATORS
 On the basis number of operands required for an operator, operators are of THREE types:

1. Unary operator
2. Binary operator
3. Ternary operator

 On the basis of utility(function) ,operators are classified into Eight types:

I. Arithmetic Operators II. Relational Operators III. Logical Operators


IV. Assignment operators V. Increment and Decrement Operators
VI. Conditional Operators VII. Bitwise Operators
VIII. Special Operators.

4
CLASSIFICATION BASED ON
OPERANDS

5
1.UNARY OPERATORS
 Operators that act upon a single operand to produce a new
value.

 Examples are :
a. unary minus(-)
The minus operator changes the sign of its argument.
A positive number becomes negative
A negative number becomes positive.

Example: int a = 1 ;
6
int b= -a ; //This means b= -1
b. increment(++)
Used to increment the value of the variable by 1.
Increment can be done by TWO ways:
 Prefix Increment:
– the operator precedes the operand(e.g.++a).
– the value of operand will be altered before it is used.
– example: int a=1;
int b=++a; //This means b= 2
 Postfix Increment
– the operator follows the operand (e.g., a++).
– the value operand will be altered after it is used.
– example: int a=1;
int b=a++; // This means b= 1 7
c. decrement(- -)
decrements the value of the variable by 1.
 The decrement can be done in two ways:
 Prefix decrement
– the operator precedes the operand (e.g.--a).
– the value of operand will be altered before it is used.
– example: int a=1;
int b=--a; //This means b= 0
 Postfix decrement
– In this method, the operator follows the operand (e.g. a--).
– The value of operand will be altered after it is used.
– example: int a=1;
int b=a--; //This means b= 1 8
d. NOT(!)
 reverses the logical state of its operand.
 if a condition is true, then Logical NOT operator will make it false and vice
versa.
example : If x is set to true, then !x is false
If x is false, then !x is true.

e. Addressof operator(&)
returns the memory address of a variable.
 These addresses returned by the address-of operator are known as pointers
because they “point” to the variable in memory.
example: int a, *ptr;
ptr=&a; //the address of integer variable a is assigned to ptr.
9
f. sizeof()
 returns the size of its operand, in bytes.
 the sizeof operator always precedes its operand.
 example: int a ;
sizeof(a); //This returns 2 bytes of memory

10
2.BINARY OPERATOR
 Requires TWO operands for the operations.
 Example:
 addition (+)
 subtraction(-)
 multiplication(*)
 division(/)
 less than(<)
 greater than(>)
 modulus operator(%)

11
3.Ternary operator
 Requires THREE arguments/operands for the operations.

 Conditional operator is an example of ternary operator.


– The operator “?:” is a conditional operator.
– Syntax:
expression1? expression2 : expression3

– Here ,the expression1 is evaluated first, if expression1 is true ,expression2 will


be final value else expression 3 will be the final value of the conditional
expression.
– This is equivalent to
if(expression1)
value=expression2;
else
12
value=expression3;
EXAMPLE:
void main() {
int a=10,b=20,c;
c= (a>b)? a:b;
printf(“ c=%d ”, c); //c=20
}

13
CLASSIFICATION BASED ON
FUNCTIONALITY

14
I. ARITHMETIC OPERATORS
Binary operators that perform arithmetic operations.

Operator Meaning Example Output(a=20,b=10)

+ Addition a+b 30
- Subtraction a-b 10

* Multiplication a*b 200

/ Division a/b 2
% Modulo Division a%b 0
15
II.RELATIONAL OPERATORS
 Binary operators that compares two similar operands.

Operator Meaning Example Output(a=10,b=20)


< Less than a<b (10<20) gives 1
> Greater than a>b (10>20) gives 0
<= Less than or equal to a<=b (10<=20)gives 1

>= Greater than or equal to a>=b (10>=20) gives 0


== Equal to a==b (10==20)gives 0
!= Not equal to a!=b (10!=20)gives 1
16
III.LOGICAL OPERATORS
 Compares or evaluates the logical and relational expressions.
 The output of these operators is always either 1(True) or 0(False).
 The logical operators are :
1. Logical AND (&&)
–returns true only when both the conditions/values are true else
false.
2.Logical OR (||)
– returns true if one or both the conditions/values are true.
3.Logical NOT (!)
–returns true if condition is false and vice versa.
17
a b a&&b a||b !a !b
0 0 0 0 1 1
0 1 0 1 1 0
1 0 0 1 0 1
1 1 1 1 0 0

18
IV.ASSIGNMENT OPERATORS
 Are used to assign result of an expression to a variable.
 The mostly used assignment operator is “=”.
 Example
a=(b*c)+3; //assigns the RHS expression’s value to a

19
• THERE ARE SOME OTHER SHORT HAND ASSIGNMENT
OPERATORS ,ALSO CALLED AS ARITHMETIC ASSIGNMENT
OPERATORS.
+= a+=b a=a+b Assigns sum of a and b to a If a=20,b=10
Then a=30
-= a-=b a=a-b Assigns subtraction of a and b If a=20,b=10
to a Then a=10

/= a*=b a=a*b Assigns multiplication of a and If a=20,b=10


b to a Then a=200

%= a%=b a=a%b Assigns modulus of a and b to If a=20,b=10


a Then a=0

20
V.BITWISE OPERATORS
 Manipulates data at bit level.
 Can be applied to only integer type operands.
 There are THREE types of bitwise operator:
1. Bitwise logical operators
2. Bitwise shift operators
3. Bitwise complement operators

21
1.BITWISE LOGICAL OPERATORS
 Performs logical tests between two integer-type operands.
 Further divided into following types:
 Bitwise AND (&)
 Bitwise OR (|)
 Bitwise XOR (^)

22
BITWISE AND OPERATOR(&)
 Performs logical ANDing between two operands.
 The result is 1 if both the bits value is 1 otherwise it is 0.
 Example:
a=60 and b=15
a 0000000000111100
b 0000000000001111
a&b = 0000000000001100

12 23
BITWISE OR OPERATOR(|)
 Performs logical ORing between two operands.
 The result is 1 if both or either of the bits value is 1 otherwise it is 0.
 Example:
a=60 and b=15
a 0000000000111100
b 0000000000001111
a|b = 0000000000111111

63 24
BITWISE XOR OPERATOR(^)
 Performs Exclusive ORing between two operands.
 The result is 1 only if one of the bits value is 1 otherwise it is 0.
 Example:
a=60 and b=15
a 0000000000111100
b 0000000000001111
a^b = 0000000000110011

51 25
2.BITWISE SHIFT OPERATORS
 Used to move bit patterns either to the left or to the right.
 Performed on binary values.
 There are TWO types of Bitwise shift operators:
 Left Shift operators(<<)
 Right Shift operators(>>)

26
BITWISE LEFT SHIFT(<<)
 The left operands value is moved left by the number of bits specified by the right operand.

 Example:
a=60; //This means, binary representation of a is 0011 1100

Then a<<2 is
1111 0000  240
(all values are shifted leftward up to 2 positions. Leftmost 0 digits get omitted due to the
shift. 2 new 0-digits are added behind)
So,
a<<2; //Produces a=240 value
27
BITWISE RIGHT SHIFT(>>)
 The left operands value is moved right by the number of bits specified by the right
operand.

 Example:
a=60; //This means, binary representation of a is 0011 1100

Then a>>2 is
0000 1111 15
(all values are shifted rightward up to 2 positions. Rightmost 0 digits get omitted due to the
shift. 2 new 0-digits are added at front)
So,
a>>2; //Produces a=15 value
28
PRECEDENCE &
ASSOCIATIVITY

29
PRECEDENCE
 If multiple operators are present in a line of C program, C determines which operator is evaluated first through
the rule of precedence.

 The operator at higher level of precedence is evaluated first and then the operator at lower level.

 There are distinct levels of precedence and an operator may belong to one of these levels.
 Generally the operators are grouped hierarchically according to their precedence.

 If two operators have same level of precedence, then C uses associativity rule to determine the direction of
operation.

30
EXAMPLE:

70

Here ,10 + 20 * 3 is calculated as 10 + (20 * 3) and not as (10 + 20) * 3


31
ASSOCIATIVITY
 Associativity is used when there are two or more operators of same precedence level is
present in an expression.

 It can be evaluated either from Left to Right or Right to Left.


 Arithmetic operators are generally evaluated from left to right.

 The natural order of evaluation can be altered through the use of parenthesis.

32
EXAMPLE:

L to R

33
• The use of parenthesis in an expression will change the order of evaluation
thereby producing the different result.
• Example + & - are Precedence level 4

x=a–b/3+c*2–d

* & / are Precedence level 3

x=10 – 12 / 3 + 3 * 2 – 1
Both / and * are of same precedence level and
are executed from L to R end.
x=10-4+6-1

x=6+6-1

x=11
After the use of parenthesis
x = a – b / (3 + c) * (2 – d)
Taking same value of a=10 ,b=12,c=3 and d=1
x=10 – 12 / ( 3 + 3 ) * ( 2 – 1)

Highest level of precedence of 1 and


L To R associative

x=10-12/6*1
x=10-2*1
x=10-2
x=8
36
Expression evaluations in C
• Expression evaluation in C is used to determine the order of the
operators to calculate the accurate output.

• Arithmetic, Relational, Logical, and Conditional are expression


evaluations in C.

• Expression evaluation is nothing but operator precedence and


associativity.
Type of expression evaluation operators
1. Arithmetic expression evaluation
 *, /, %, +, - (left to right)

2. Relational expression evaluation


<, <=, >, >=, ==, != (left to right)

3. Logical expression evaluation


 &&, ||, ! (left to right)

4. Conditional expression evaluation


? : (left to right)
End of chapter 4
Related questions

• Explain in detail the classification of operators that are used in C


programming
• For assignment purpose, Define each type of operator with example.

• Write short notes on:


a) Precedence & associativity for expression evaluation
b) Sizeof() operator
c) Unary, binary and ternary operator

You might also like