0% found this document useful (0 votes)
11 views47 pages

Unit 1 - III Operators and Expressios

The document discusses various operators in C programming. It describes arithmetic operators like addition, subtraction, multiplication, division, and modulus. It also covers relational operators, logical operators, assignment operators, increment/decrement operators, conditional operators, and bitwise operators. Examples are provided to demonstrate the usage and behavior of each operator type.

Uploaded by

Thomas Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views47 pages

Unit 1 - III Operators and Expressios

The document discusses various operators in C programming. It describes arithmetic operators like addition, subtraction, multiplication, division, and modulus. It also covers relational operators, logical operators, assignment operators, increment/decrement operators, conditional operators, and bitwise operators. Examples are provided to demonstrate the usage and behavior of each operator type.

Uploaded by

Thomas Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

1

Operators & expressions in C


C PROGRAMMING
Classes of C Operators 2

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
1. Arithmetic operators 3

Operator Meaning

+ Addition

– Subtraction

* Multiplication

/ Division

% Modulus Operator/modulo division operator/remainder operator


Integer Arithmetic 4
Let x = 27 and y = 5 (Assume x , y , z are integers)

❑ z=x+y 🡺 32 // x, y are operands

❑ z=x–y 🡺 22

❑ z=x*y 🡺 135

❑ z=x/y 🡺5

❑ z=x%y 🡺 2 , 10%2 ⇒ ? , 13%5⇒ ?


Modulo division/modulus operator (%) 5

❑ Works only for integers.


❑ x%y gives the remainder of integer
division x/y.
❑ if x=10, y= 3 then x%y = 1
❑ 10%5 = 0
❑ 13%8 = 5
❑ -10%4 = -2
❑ 10%-4 = 2
❑ -10%-4 = -2 (5%10 ⇒5)
❑ Sign of Modulo division result is sign of first operand.
Floating point arithmetic 6

Let real nos. 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.
Mixed mode arithmetic 7

❑ Involving different data types as operands.


(Eg. Integer & floating point numbers)
❑ 15/10.0 = ??? ⇒ 1.5
❑ 15/10 = ??? ⇒ 1

❑ int c = 15/10.0;
❑ c = ?? ⇒ 1
❑ float f = 15/10; // 1
❑ f = ?? ⇒ 1.0
2. Relational Operators 8

❑ Used to compare two data values for taking decisions.

❑ < 🡺 less than


❑ <= 🡺 less than or equal to
❑ > 🡺 greater than
❑ >= 🡺 greater than or equal to
❑ == 🡺 equal to
❑ != 🡺 not equal to
2. Relational Operators 9

❑ Two possible outcomes for a relational


operator. (e.g: a >=b)

❑ 1. TRUE 🡺 Represented by 1

❑ 2. FALSE 🡺 Represented by 0
2. Relational Operators 10
❑ Example usage of relational operator:
int main(){
int age = 20;
if( age >= 18)
{
printf("Person can vote");
}
else
{
printf("Person can not vote");
}
return 0;
}
Logical Operators 11

Operator Meaning

&& Logical AND


|| Logical OR
! Logical NOT

❑ Mainly used to check more than one condition to take


decisions.
Logical Operators 12

► Example usage: LOGICAL AND operator(&&)


int age = 25 ,salary = 30000;
if( (age < 30) && (salary>25000)) // T && T ⇒ T
{
printf(“Person is well paid”);
}
else
{
printf(“Person is under-paid”);
}
Logical Operators 13

► 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
Logical Operators 14

► Example usage: LOGICAL OR operator(||)


int age = 25;
if(age < 13 || age >60) // T|| T ⇒ T
{
printf(“Need special care\n”);
}
else
{
printf(“No need of special care\n”);
}
Logical Operators 15

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
Truth table of logical NOT operator 16

Syntax: !Exp
e.g !x , !0, !5, !(x>5)

Exp !Exp
====================
NZ 0
0 1
Logical Operators 17

LOGICAL NOT operator(!) !(a<b)


► A unary operator.
► Negates the operands value.
int a = 10;
a<18; // output: 1
!(a<18); // output: 0
► Normally Used in Decision making
if( age>=18) // age>=18 ~ !(age<18)
printf(“Person can vote.”);
► !(age<18) is equivalent to age>=18. Either can be used in
program.
Assignment Operators 18

Assignment Operators:
❑ = 🡺 Assign value of RHS expression to variable on
LHS
E.g: a = 5; b = c+5; d = a+b*c; b = b+5;
❑ Shorthand Assignment Operators
❑ General Syntax : v op= exp;
❑ += , -=, *=, /=, %=
❑ E.g: b+=5; a*=b; a%=3;
Assignment Operators 19

Assignment Shorth and Assgn.


❑ 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
Assignment Operators 20

Shorthand assignment operator Advantages


❑ LHS need not be repeated (instead of a= a+5
simply a+=5)
❑ Statement is concise.
❑ Statement is more efficient. (Improves speed of
execution)
Increment & Decrement Operators 21

❑ ++, -- 🡺 Unary operators. a = a-1; ⇒ --a


❑ ++ 🡺 Increase value of variable by 1. E.g: a = a+1; ⇒ ++a,
❑ 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;
Increment & Decrement Operators 22

Examples:
❑ m = 5;
❑ y = ++m; // (prefix) // m=m+1; y = m;
❑ After Execution:
❑ result ⇒ y=6, m=6
❑ m = 5;
❑ y = m++; // (postfix) // y = m; m=m+1
❑ After Execution:
❑ y=5, m=6
Increment & Decrement Operators 23

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.
Conditional Operator ( ?: ) 24

❑ General Syntax: exp1?exp2:exp3;



❑ Exp1 is evaluated first.
❑ If exp1 evaluates to a non-zero value(true) then exp2 is
evaluated.
❑ If exp2 evaluates to a zero (false) then exp3 is evaluated.
❑ Same as:
if(exp1)
exp2;
else
exp3;
Conditional Operator 25

❑ Example 1:
❑ a = 10; b=15;
❑ x = (a>b)?a:b; // x = b; 🡺 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.
❑ z = x<0?x * -1: x; // Finds absolute value of x
Bitwise Operators 26
❑ Act on bit level in a data item(integer variable, int constant
etc.)
❑ Can’t work on floating point numbers(real numbers)
❑ Operators are:
<< 🡺Shift left
>> 🡺Shift right
& 🡺 Bitwise AND
| 🡺 Bitwise OR
^ 🡺Bitwise Exclusive OR
~ 🡺 Bitwise complement
Computer Memory(RAM) layout 27

● Size of datatype/variable refers to number of bytes occupied in memory.

int y = 2; 31(decimal) =
0000000000011111(binary)

1(decimal) =
8 bits = 1 Byte
0000000000000001(binary)
1024 bytes = 1 KiloByte(KB)
1024 KB = 1 Mega Byte(MB)
1024 MB = 1 Giga Byte(GB)
1024 GB = 1 Tera Byte (TB) 1006
1005
1004
y 00000010 1003
1002
00000000 1001
x
00000001 1000
View of RAM/ Memor
primary memory
y Addr
Bitwise Operators (<<) 28

❑ 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??
Bitwise Operators(>>) 29

❑ 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?
Bitwise Operators(&) 30

❑ Examples(Bitwise AND - &): (Assume integer


variable is 1 byte size)
❑ int a = 5,b = 6,c;
❑ a‘s Bit Sequence in Memory: 0 0 0 0 0 1 0 1
❑ b‘s Bit Sequence in Memory: 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
Bitwise Operators(|) 31

❑ Examples(Bitwise OR: | ): (Assume integer


variable is 1 byte size)
❑ int a = 5,b = 6,c;
❑ a’s Bit Sequence in Memory: 0 0 0 0 0 1 0 1

❑ b’s Bit Sequence in Memory: 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
Bitwise Operators(^) 32

❑ Examples(Bitwise XOR:^): (Assume


integer variable is 1 byte size)
❑ int a = 5,b = 6,c; // b = ~a
❑ a's Bit Sequence in Memory: 0 0 0 0 0 1 0 1

❑ b's Bit Sequence in Memory: 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
Special Operators:Comma(,) 33

1. 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++)
Special Operators: sizeof 34

2. 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??
Arithmetic Expressions 35
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
36
Evaluation of expressions

❑ General format:
variable = expression; // 5, a, x*a+5
❑ 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;
Precedence of Arithmetic Operators 37

❑ int a = 2,b =4,c=5,d=3;


❑ int e = a+b*c/d-c; // m first 8 , d first 6
❑ 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: + -
Operator Precedence 38

❑ 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
Precedence & Associativity 39
❑ Associativity :- Defines the direction of execution
when operators of same precedence level are
involved.
❑ Two types :
Left-to-right associativity (L-R)
Right-to-left associativity (R-L)
❑ Arithmetic operators follow L-R associativity.
E.g.: a + b * c - d ⇒ a+z-d

❑ Precedence rules can be bypassed with use of


parenthesis.
❑ E.g. (a+b)*c-d
Precedence &
Associativity
Precedence & Associativity 41

❑ 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.
Precedence & Associativity of most 42
common operators
Operator Category Associativity
*,/,% Arithmetic L to R
operators
Arithmetic L to R
+,- operators
<,<=,>,>= 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
‘Type conversion’ in expressions 43
❑ 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
► Implicit/Automatic Type Conversion
► Explicit 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.
Entire Data Types in C 44
Implicit Type Conversion hierarchy 45
Type conversion in expressions 46
❑ 2. Explicit Type Conversion: Explicitly done using casting
operator
► variable = (datatype) 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


47

End

You might also like