1
Operators &
Expressions in C
C Programming
Classes of C Operators
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increments and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
2
1. Arithmetic operators
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo(Reminder value)
3
Integer Arithmetic
Let, x = 27 and y = 5
• z=x+y 32
• z=x–y 22
• z=x*y 135
• z=x/y 5
• z=x%y 2
4
Floating point arithmetic
Let x = 14.0 and y = 4.0 then,
• z=x+y 18.0
• z=x–y 10.0
• z=x*y 56.0
• z=x/y 3.50
• Modulo división can not be applied for floating point numbers.
5
Mixed mode arithmetic
• Involving different data types as operands. (Eg. Integer & floating point numbers)
• 15/10.0 = ???
• 15/10 = ???
• int c = 15/10.0;
• c = ?? //1
• float f = 15/10;
• F = ?? //1.5
6
2. Relational Operators
• Used to compare two data values for taking decisions.
Operator Meaning
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= Not equal to
7
2. Relational Operators
• Two possible outcomes for a relational operator. (a >=b)
• 1. TRUE Represented by 1
• 2. FALSE Represented by 0
8
2. Relational Operators
• Example usage:
void main()
{
int age = 5;
if(age > 18)
{
printf(“Person can vote”);
}
else
{
printf(“Person can not vote”);
}
9
}
Logical Operators
Mainly used to check more than one condition to take decisions.
Operator Meaning
&& Logical AND
|| Logical OR
! Logical Not
10
Logical Operators
Output(Truth table) of LOGICAL AND operator(&&)
Eg: Exp1 && Exp2
EXP1 EXP2 EXP1&&EXP2
0 0 0
0 NZ 0
NZ 0 0
NZ NZ 1
NZ Non-zero
For TRUE output both expressions must be NON-ZERO
11
Logical Operators
Example usage: LOGICAL AND operator(&&)
int age = 25,salary = 30000;
if(age < 30 && salary>25000)
{
printf(“Person is well paid”);
}
else
{
printf(“Person is under-paid”);
}
12
Logical Operators
Output(Truth table) of LOGICAL OR operator(||)
Eg: Exp1 || Exp2
Exp1 Exp2 Exp1 || Exp2
0 0 0
0 NZ 1
NZ 0 1
NZ NZ 1
NZ Non-zero
For TRUE output, either of the expressions only need to be NON-ZERO
13
Logical Operators
Example usage: LOGICAL OR operator(||)
int age = 5;
if(age < 13 || age >60)
{
printf(“Need special care\n”);
}
else
{
printf(“No need of special care\n”);
}
14
Logical Operators
LOGICAL NOT operator(!)
A unary operator.
Negates the operands value.
int a = 10;
a<18; // output: 1
!(a<10); // output: 0
Normally Used in Decision making
if( !(age<18) )
printf(“Person can vote.”);
!(age<18) is equivalent to age>=18. Either can be used in program.
15
Assignment Operators
Assignment Operators are
• = Assign value of RHS expression to variable on LHS
• E.g: a=5; b= c+5; d = a+b*c;
• Shorthand Assignment Operators
• General Syntax : v op= exp;
• += , -=, *=, /=, %=
• E.g: a+=5; a*=b; a%=3;
• A=a+5 a+=5
• A=a*b a*=b
16
Assignment Operators
Regular Shorthand
• a=a+1 a += 1
• a=a–1 a -= 1
• a = a * (n+1) a *= (n+1)
• a = a / (n+1) a /= (n+1)
• a=a%b a %= b
• a=a/(n-2) a/=(n-2)
• b=b%2 b%=2
• c*=4 c=c*4
17
Assignment Operators
Shorthand assignment operator Advantages
• LHS need not be repeated (instead of a= a+5 simply a+=5)
• Statement is concise.
• Statement is more efficient.
18
Increment & Decrement Operators
• ++, - - Unary operators.
• ++ Increase value of variable by 1.
• Can only be applied to variables(Not on constants or expressions)
• Usage: (Let v be a variable)
• 1. ++v Prefix Increment : same as v = v+1;
• 2. v++ Postfix Increment : same as v = v+1;
• 3. --v Prefix Decrement : same as v = v-1;
• 4. v-- Postfix Decrement : same as v = v-1;
19
Increment & Decrement Operators
Examples:
• m = 5;
• y = ++m; (prefix) m=6
• After Execution:
• y=6, m=6
• m = 5;
• y = m++; (postfix) // y=m=5++1 m=6;y=5
• After Execution:
• y=5, m=6
20
Increment & Decrement Operators
Rules:
• ++,-- are Unary operators, these work only on variables.
• Postfix ++(--)used in expression first value is used in evaluation of
expression, then incremented(decremented).
• Prefix ++(--)used in expression first variable
incremented(decremented), the new value is used in evaluation of
expression,
• Precedence and associativity of ++ and – are same as that of unary +
and unary –
21
Conditional Operator (?:)
• General Syntax: exp1?exp2:exp3;
• Exp1 is evaluated first.
• If exp1 evaluates to a non-zero value(true) then exp2 is evaluated.
• If exp1 evaluates to a zero (false) then exp3 is evaluated.
• Same as:
if(exp1) (age<18) ? Printf(“U can not vote); : printf(“U can vote);
exp2;
else
exp3; 22
Conditional Operator
• Example 1:
• a = 10; b=15;
• x = (a>b)?a:b; // x = 15; Finding larger of two.
• Same as:
if(a>b)
x = a;
else
x = b;
• A concise way of doing if-else condition.
• Reduces number of lines of code. 23
Bitwise sequence
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
128 64 32 16 8 4 2 1
0 0 0 0 1 0 1 0
10
11 1 1 1
24
Bitwise Operators
• Act on bit level in a data item(integer variable, int constant etc.)
• Can’t work on floating point number(real numbers)
• Operators are:
• << Shift left
• >> Shift right
• & Bitwise AND
• | Bitwise OR
• ^ Bitwise Exclusive OR
25
Bitwise Operators(<<)
• Examples(shift left: <<): (Assume integer variable is 1 byte size)
• int a = 5;
• a’s Bit Sequence in Memory:
0 0 0 0 0 1 0 1
• Operators are:
• b = a<<1; // Shift the bits one position left and assign to b.
• b's Bit Sequence in Memory:
0 0 0 0 1 0 1 0
• a's bit sequence remains same as original.
• Values of a and b??
26
128 64 32 16 8 4 2 1
15 0 0 0 0 1 1 1 1
30 0 0 0 1 1 1 1 0
60 0 0 1 1 1 1 0 0
a= 15
b = a<<2;
27
Bitwise Operators(>>)
• Examples(shift right >>): (Assume integer variable is 1 byte size)
• int a = 5;
• a‘s Bit Sequence in Memory:
0 0 0 0 0 1 0 1
• Operators are:
• b = a>>1; // Shift the bits one position right and assign to b.
• b‘s Bit Sequence in Memory:
0 0 0 0 0 0 1 0
• a‘s bit sequence remains same as original.
• Values of a & b?
28
128 64 32 16 8 4 2 1
15 0 0 0 0 1 1 1 1
7 0 0 0 0 0 1 1 1
3 0 0 0 0 0 0 1 1
a=15
b= a >> 2
29
Bitwise Operators(&)
• Examples(Bitwise AND :&): (Assume integer variable is 1 byte size)
• int a = 5,b = 6,c;
• a‘s Bit Sequence in Memory:
• b‘s Bit Sequence in Memory: 0 0 0 0 0 1 0 1
0 0 0 0 0 1 1 0
• c = a &b;
• c‘s Bit Sequence in Memory:
0 0 0 0 0 1 0 0
30
• int a =10 , b= 11,c;
• c= a& b ;
10 0 0 0 0 1 0 1 0
11 0 0 0 0 1 0 1 1
c=a& 1 0 1 0
b
31
Bitwise Operators(|)
• Examples(Bitwise OR:|): (Assume integer variable is 1 byte size)
• int a = 5,b = 6,c;
• a’s Bit Sequence in Memory:
• b’s Bit Sequence in Memory: 0 0 0 0 0 1 0 1
0 0 0 0 0 1 1 0
• c = a | b;
• c‘s Bit Sequence in Memory:
0 0 0 0 0 1 1 1
32
20 0 0 0 1 0 1 0 0
30 0 0 0 1 1 1 1 0
C=a||b 0 0 0 1 1 1 1 0
33
Bitwise Operators(^)
• Examples(Bitwise XOR:^): (Assume integer variable is 1 byte size)
• int a = 5,b = 6,c;
• a's Bit Sequence in Memory:
• b's Bit Sequence in Memory: 0 0 0 0 0 1 0 1
0 0 0 0 0 1 1 0
• c = a ^ b;
• c‘s Bit Sequence in Memory:
0 0 0 0 0 0 1 1
34
10 0 0 0 0 1 0 1 0
11 0 0 0 0 1 0 1 1
1 0 0 0 0 0 0 0 1
35
Bitwise not operator(~)
• The complement operator (~) is a unary prefix operator and is used, as in ~a.
• It takes one number and inverts all bits of it
36
37
Special Operators:Comma(,)
• Comma (,)
• Used to link related expressions together.
• Comma separated expressions execute from left to right.
• Right most expression is the value of the combined expression.
• E.g: v = (x=10, y=5, x+y);
• Value of v?
• E.g. for loop: for(i=0,j=1; i<n; i++,j++)
38
Special Operators:sizeof
• Size of operator(sizeof)
• Used to find the size(in bytes) of a data type or variable or constant.
• int a = sizeof(int); // a??
• int b = sizeof(float); // b??
• int c = sizeof(a); // c??
• int d = sizeof(100); // d??
39
Arithmetic Expressions
AE is a combination of variables, constants and operators arranged as per syntax of the language
• Algebraic expression • C Expression
– axb–c a*b-c
– (m+n)(x+y) (m+n)*(x+y)
– (ab)/c (a*b)/c
– 3x2 + 2x+1 3*x*x+2*x+1
– (x/y) + c x/y+c
40
Evaluation of expressions
• General format:
variable = expression;
• First expression is evaluated, the result is assigned to variable in LHS.
• x = a * b - c;
• y = b / c *a;
• z = a – b / c + d;
41
Precedence of Arithmetic Operators
• int a = 2,b =4,c=5,d=3;
• int e = a+b*c/d;
• The order of executing sub-expressions affects the final result.
• C Language defines rules for deciding the order execution in such
complex expression.
• Precedence: refers to the priority of operators w.r.t choosing order of
execution.
• High Precedence: * / %
• Low Precedence: + -
42
Operator Precedence
• All operators are classified into different levels of precedence.
• If expression involves multiple operators( e.g: a+b*c-d), operation with
higher precedence is performed first.
• ‘b*c’ is done first in the example, since * has higher precedence than +,-
• What if the precedence for two operators are same? ?
• I.e, in example a+b*c-d , + and – have the same precedence.
• Associativity
43
Precedence & Associativity
• Associativity :- Defines the direction of execution when operators of same
precedence level are involved.
• Two types :
Left-to-right associativity
Right-to-left associativity
• Arithmetic operators follow L-R associativity.
E.g.: a + b * c - d
• Precedence rules can be bypassed with use of parenthesis
• E.g. (a+b)*c-d 44
Precedence &
Associativity
45
Precedence & Associativity
• E.g. a+b*c-e*f
• E.g. if(a>b && c==d)
• E.g. a=5, b=10; !a<b (Note: ! has higher precedence than <)
• (a*b)*(c-d)/(e+f)
• Precedence rules decide the order in which different precedence level operators
are applied.
• Associativity rules decide the order in which operators in same precedence level
are applied.
46
Precedence & Associativity of most
common operators
Operator Category Associativity
*,/,% Arithmetic operators L to R
+,- Arithmetic operators L to R
<,<=,>,>= Relational Ops L to R
==,!= Relational Ops L to R
&& Logical AND L to R
|| Logical OR L to R
=,*=,/=, %=,+=,-= Assignment & R to L
Short hand
47
‘Type conversion’ in expressions
• Conversion of one data type to another.
• Done when expressions contains operands of different types.
• E.g: 2+3*2.7+7
• Two types of type conversion
• 1. Implicit Type Conversion: Automatically done by the compiler.
• Conversion happens according to the rules of the language.
• General thumb rule for ‘automatic’ type conversion: Conversion done such a
way that, there is no data/precision loss for the data.
48
Entire Data Types in C
49
Implicit Type Conversion hierarchy
50
Type conversion in expressions
• 2. Explicit Type Conversion: Explicitly done using casting operator
– variable = (type)expression; // variable/expression
• Conversion happens according to the rules of the language.
int a =10;
float b;
b = (float)a; // explicit type conversion
• Another example
int a = 5,b =3;
float c = a/b; // c = 1.0
float d = (float)a/b; // d = 1.666667
51
Examples of output statements
printf(“Hello world..!!”);
• Output: Hello world..!!
int a = 5,b=3;
float c = (float)a/b;
printf(“Result of %d / %d is %f”,a,b,c);
• Output: Result of 5 / 3 is 1.666667
printf(“Result of %d / %d is \n %f”,a,b,c);
• Output: Result of 5 / 3 is
1.666667
52
Examples of Input statements
int a,b;
float c;
char d;
double e;
scanf(“%d %d %f”,&a,&b,&c);
scanf(“%c”,&d);
scanf(“%lf”,&e);
Wrong way: scanf(“c=%c\n”,&d); // Wrong!!
53
• 34+24/12%3*5/2-4
54
• int a=10,b=3;
• printf("%d",a<<b);
• A=10 – 00001010
• B=3
• A<<b
A<<1 – 00010100
A<<1 – 00101000
A<<1 - 01010000
55