Professional Documents
Culture Documents
Chapter 4: Operators and Expressions
Chapter 4: Operators and Expressions
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
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.
13
CLASSIFICATION BASED ON
FUNCTIONALITY
14
I. ARITHMETIC OPERATORS
Binary operators that perform arithmetic operations.
+ Addition a+b 30
- Subtraction a-b 10
/ Division a/b 2
% Modulo Division a%b 0
15
II.RELATIONAL OPERATORS
Binary operators that compares two similar operands.
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
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
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
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)
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.