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

L3-Operators and expressions

C lecture pdf 3

Uploaded by

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

L3-Operators and expressions

C lecture pdf 3

Uploaded by

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

C Programming

Module-3: Operators and Expressions in C

IMT and IMG 1st Year

Instructor : Dr. Santosh Singh Rathore


ABV-IIITM Gwalior
Disclaimer

This is NOT A COPYRIGHT MATERIAL

Content for the slides have been taken from the various sources and
from the reference books.

Students need to follow reference books to get the thorough details


of the concepts discussed in slides
Operators
• C includes a large number of operators that fall under several different
categories, which are as-
• Arithmetic operators
• Assignment operators
• Increment and Decrement operators
• Relational operators
• Logical operators
• Conditional operator
• Comma operator
• sizeof operator
• Bitwise operators
• Other operators
Arithmetic Operators
• Arithmetic operators are used for numeric calculations. They are of two types -
• Unary arithmetic operators: Unary operators require only one operand
• Binary arithmetic operators: Binary operators require two operands.

• There are five binary arithmetic operators

• % (modulus operator) cannot be applied with floating point operands.


Integer Arithmetic
• When both operands are integers then the arithmetic operation with these operands is
called integer arithmetic and the resulting value is always an integer.

• After division operation the


decimal part will be truncated
and result is only integer part of
quotient.
• After modulus operation the
result will be remainder part of
integer division.
Floating-point Arithmetic
• When both operands are of float type then the arithmetic operation with these operands
is called floating-point arithmetic:
Mixed Mode Arithmetic
• When one operand is of integer type and the other is of floating type then the arithmetic
operation wi1 these operands is known as mixed mode arithmetic and the resulting
value is float type.
Assignment Operators
• A value can be stored in a variable with the use of assignment operator.
• This assignment operator “ = ", is used in assignment expressions and assignment
statements.
• The operand on the left hand side should be a variable, while the operand on the right
hand side can any variable, constant or expression.
• The value of right hand operand is assigned to the left hand operand.
• x = 8 /* 8 is assigned to x*/
• y = 5 /* 5 is assigned to y*/
• s = x+y-2 /* Value of expression x+y-2 is assigned to s*/
• y = x /* Value of x is assigned to y*/
• x = y /* Value of y is assigned to x*/
• We can have multiple assignment expressions also, for example
• x = y = z = 20, Here all the three variables x, y, t will be assigned value 20
Assignment Operators
• When the variable on the left hand side of assignment operator also occurs on right
hand side then we can avoid writing the variable twice by using compound
assignment operators.
• For example x = x + 5
• can also be written as x += 5
• Here += is a compound assignment operator.
• Similarly we have other compound assignment operators
• x -=5 is equivalent to x = x - 5
• y*=5 is equivalent to y = y* 5
• sum/=5 is equivalent to sum = sum / 5
• k%= 5 is equivalent to k = k % 5
Increment and Decrement Operators
• C has two useful operators increment ( ++ ) and decrement ( - - ).
• These are unary operators because they operate on a single operand.
• The increment operator ( ++ ) increments the value of the variable by 1 and decrement
operator ( - - ) decrements the value of the variable by 1.
• ++x is equivalent to x = x + 1
• - -x is equivalent to x = x-1
• These operators should be used only with variables; they can't be used with constants or
expressions.
• For example the expressions ++5 or ++(x+y+z) are invalid.
• These operators are of two types-
• Prefix increment / decrement - operator is written before the operand (e.g. ++x or - -x )
• Postfix increment / decrement - operator is written after the operand (e.g. x++ or x - - )
Prefix Increment / Decrement
• Here first the value of variable' is incremented / decremented then the new value is used
in the operation
• Let us take a variable x whose value is 3.
• The statement y = ++x; means first increment the value of x by 1, then assign the value
of x to y

• This single
statement is -
equivalent to
these two
statements
• x = x+l;
• Y = x;
Output:
x = 8 x = 9 x = 9 x = 8 x = 8, In the second printf statement, first the value of x is incremented and then
printed; similarly in the for printf statement first the value of x is decremented and then printed.
Postfix Increment/Decrement
• Here first the value of variable is used in the operation and then increment/decrement is
performed.
• The statement y = x++; means first the value of x is assigned to y and then x is
incremented.
Relational Operators
• Relational operators are used to compare values of two expressions depending on their
relations.
• An expression that contains relational operators is called relational expression.
• If the relation is true then the value of relational expression is 1 and if the relation is
false then the value of expression is 0.
Relational Operators
• Let us take· two variables a = 9 and b = 5, and form simple relational expressions with
them
Logical Or Boolean Operators
• An expression that combines two or more expressions is termed as a logical expression.
• For combining these expressions we use logical operators.
• These operators return 0 for false and 1 for true. The operands y be constants, variables
or expressions.

• Here logical NOT is a unary operator while the other two are binary operators.
• In C any non-zero value is regarded as true and zero is regarded as false .
AND ( &&) and OR (||) Operator
• AND operator gives the net result true if both the conditions are true, otherwise the
result is false.

• OR operator gives the net result false, if both the conditions have the values false,
otherwise the result is true.
NOT (!) Operator
• This is a unary operator and it negates the value of the condition.
• If the value of the condition is false then it gives the result true.
• If the value of the condition is true then it gives the result false.
Conditional Operator
• Conditional operator is a ternary operator (? and:) which requires three expressions as
operands.
• This written as TestExpression ? expression1 : expression2
• Firstly the TestExpression is evaluated.
• If TestExpression is true (nonzero), then expression1 is evaluated and it becomes the
value of the overall conditional expression.
• If TestExpression is false (zero), then expression2 is evaluated and it becomes the value
of overall conditional expression.
• For example consider this conditional expression a>b?a:b
• Here first the expression a > b is evaluated, if the value is true then the value of variable
a becomes the value of conditional expression otherwise the value of b becomes the
value of conditional expression.
• a < b ? printf("a is smaller") : printf("b is smaller");
• Since the expression a < b is true, so the first printf function is executed.
Comma Operator
• The comma operator (,) is used to permit different expressions to appear in situations
where only one expression would be used. The expressions are separated by the comma
operator.
• The separated expressions are evaluated from left to right and the type and value of the
rightmost expression is the type and value of the compound expression.
• For example consider this expression a = 8, b = 7, c = 9, a+b+c
• Here we have combined 4 expressions. Initially 8 is assigned to the variable a, then 7 is
assigned to the variable b, 9 is assigned to variable c and after this a+b+c is evaluated
which becomes the value of whole expression. So the value of the above expression is
24.
• Now consider this statement sum = ( a = 8, b = 7, c = 9, a+b+c );
• Here the value of the whole expression on right side will be assigned to variable sum
i.e. sum will be assigned value 24.
• Since precedence of comma operator is lower than that of assignment operator hence
the parentheses are necessary here.
sizeof Operator
• sizeof is an unary operator. This operator gives the size of its operand in terms of bytes.
• The operand can be a variable, constant or any datatype( int, float, char etc).
• For example sizeof(int) gives the bytes occupied by the int datatype i.e.2
Bitwise Operators
• C has the ability to support the manipulation of data at the bit level.
• Bitwise operators are used for operations on individual bits. Bitwise operators operate
on integers only.
Bitwise Operators
• The & (bitwise AND) in C takes two numbers as operands and does AND on
every bit of two numbers. The result of AND is 1 only if both bits are 1.
• The | (bitwise OR) in C takes two numbers as operands and does OR on every
bit of two numbers. The result of OR is 1 if any of the two bits is 1.
• The ^ (bitwise XOR) in C takes two numbers as operands and does XOR on
every bit of two numbers. The result of XOR is 1 if the two bits are different.
• The << (left shift) in C takes two numbers, left shifts the bits of the first
operand, the second operand decides the number of places to shift.
• The >> (right shift) in C takes two numbers, right shifts the bits of the first
operand, the second operand decides the number of places to shift.
• The ~ (bitwise NOT) in C takes one number and inverts all bits of it.
Bitwise Operators
// C Program to demonstrate use of bitwise operators
#include <stdio.h>
int main()
{
// a = 5(00000101), b = 9(00001001) Output:
unsigned char a = 5, b = 9;
a = 5, b = 9
// The result is 00000001 a&b = 1
printf("a = %d, b = %d\n", a, b); a|b = 13
printf("a&b = %d\n", a & b); a^b = 12
// The result is 00001101 ~a = 250
printf("a|b = %d\n", a | b); b<<1 = 18
b>>1 = 4
// The result is 00001100
printf("a^b = %d\n", a ^ b);

// The result is 11111010


printf("~a = %d\n", a = ~a);

// The result is 00010010


printf("b<<1 = %d\n", b << 1);

// The result is 00000100


printf("b>>1 = %d\n", b >> 1);

return 0;
Type Conversion
• C provides the facility of mixing different types of variables and constants in an
expression.
• In these types of operations data type of one operand is converted into data type of
another operand.
• This is known as type conversion.

• Implicit Type Conversions


• These conversions are done by the C compiler according to some predefined rules of C
language.
• There are two types of implicit type conversions are automatic type conversions and
type conversion in assignment.
• Automatic unary conversions
• All operands of type char and short will be converted to int before any operation.
• Some compilers convert all float operands to double before any operation.
Automatic binary conversions
• If one operand is long double, then the other will be converted to long double, and the
result will be long double,
• Otherwise if one operand is double, then the other will be converted to double and the
result will be double,
• Otherwise if one operand is float, the other will be converted to float and the result will be
float
• Otherwise if one operand is unsigned long int, then other will be converted to unsigned
long int and the result will be unsigned long int.
• Otherwise if one operand is long int and other is unsigned int
• If long int can represent all the values of an unsigned int, then unsigned int will be convert to
long int and the result will be long int,
• Else both the operands will be converted to unsigned long int and the result will be unsigned
long int,
• (vi) Otherwise if one operand is long int, then the other will be converted to long int and
the result will be long int.
• Otherwise if one operand is unsigned int, then the other will be converted to unsigned int
and the result will be unsigned int.
• Otherwise both operands will be int and the result will be int.
Automatic binary conversions
Type Conversion In Assignment
• If the types of the two operands in an assignment expression are different, then
the type of the right hand side operand is converted to the type of left hand
operand.
• Here if the right hand operand is of lower rank then it will be promoted to the
rank of left hand operand, and if it is of higher rank then it will demoted to the
rank of left hand operand.
• Some consequences of these promotions and demotions are-
• 1. Some high order bits may be dropped when long is converted to int, or int is
converted to short int or char.
• 2. Fractional part may be truncated during conversion of float type to lnt type.
• 3. When double type is converted to float type, digits are rounded off.
• 4. When a signed type is changed to unsigned type, the sign may be dropped.
• 5. When an int is converted to float, or float to double there will be no increase in
accuracy or precision.
Type Conversion In Assignment
• / * P4 .10 Program to understand the type conversion in assignment· /
#include<stdio.h>
main() { Output:
char c1,c2; int il,i2; float f1, f2; c2 = P, i2 = 12
c1='H'; f2 = 80.00, i2 = 72
i1=80. 56; /*Demotion: float converted to int, only 80 .assigned to il */
f1=12.6;
c2=i1; // Demotion: int converted to char
i2=f1; // Demotion: float converted to int
// Now C2 has character with ASCII value 80, i2 is assigned value 12
printf("c2 = %c, i2 = %d\n”, c2,i2);
f2=i1; \\ Promotion: int converted to float
i2= c1; \\ Promotion: char converted to' int
// Now i2 contains ASCII value of character ‘H’ which is 72
printf(“f2 = %.2f, i2 = %d\n”, f2,i2); }
Explicit Type Conversion Or Type Casting
• There may be certain situations where implicit conversions may not solve our
purpose. For example

• float z;
• int x = 20, y = 3;
• z = x/y;

• The value of z will be 6.0 instead of 6.66.


• In these types of cases we can specify our own conversions known as type
casting or coercion. This is done with the help of cast operator.
• The cast operator is a unary operator that is used for converting an expression to
a particular data type temporarily.
• The expression can be any constant or variable.
• The syntax of cast operator is (datatype) expression
Explicit Type Conversion Or Type Casting

• Here the datatype along with the parentheses is called the cast operator.
• So if we write the above statement as z = (float)x/y;
• Now the value of z will come out be 6.66.
• This happens because the cast operator (float) temporarily converted the int
variable x into float type and so floating point arithmetic took place and
fraction part was not lost.
Explicit Type Conversion Or Type Casting
• Initially the expression x/y is evaluated, both x and y are integers so according
to integer arithmetic after division, decimal value is truncated and result is
integer value 2.
• This value will be assigned to p but p is a float variable so according to implicit
type conversion in assignment the integer value 2 will be converted to float and
then assigned to p.
• So finally the value of p is 2.0
• When cast operator is used, floating point arithmetic is performed hence the
value of q is 2.5.
Precedence
and
Associativity
of Operators
Precedence And Associativity Of Operators
(i) x = a+b < c
• Here + operator has higher precedence than < and =, and < has more
precedence than =, so first a+b will be evaluated, then < operator will be
evaluated, and at last the whole value will be assigned to x. If initial values are
a = 2,b = 6, c = 9, then final value of x will be 1.

(ii) x *= a + b
• Here + operator has higher precedence than *=, so a+b will be evaluated before
compound assignment. This is interpreted as x = x*(a+b) and not as x = x*a+b.
• If initial values are x = 5, a = 2, b = 6, then final value of x will be 13.

(iii) x = a<=b II b==c


• Here order of evaluation of operators will be <=, = =, 11,=. If initial values are a
= 2, b = 3, c = 4, then final value of x will 'be 1.
Role Of Parentheses- In Evaluating Expressions

• If we want to change the order of precedence of any operation, we can use


parentheses.
• According to the parentheses rule, all the operations that are enclosed within
parentheses are performed first.
• For example in expression 24/2+4, division will take place before addition
according to precedence rule but if we enclose 2+4 inside parentheses then
addition will be performed first: So the value of expression 24/(2+4) is 4.
Order Of Evaluation Of Operands
Thank you for your attention!

Questions?

You might also like