L3-Operators and expressions
L3-Operators and expressions
Content for the slides have been taken from the various sources and
from the reference books.
• 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);
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.
• float z;
• int x = 20, y = 3;
• z = x/y;
• 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.
Questions?