Expressions
Expressions
5
Arithmetic operators
6
Example Program
#include <stdio.h>
int main()
{
int a;
a = 2 * 3.25;
printf("the result of evaluation is %d",a);
return 0;
}
7
Example Program
#include <stdio.h>
int main()
{
int a;
a = 4 % 3;
printf("the value of a is %d",a);
return 0;
}
10
Relational operators
12
Example Program
#include <stdio.h>
int main()
{
int a;
a=2<3;
printf("the value of a is %d",a);
return 0;
}
Note: Relational operators will produce 1 if the condition is TRUE else it will produce 0.
14
Logical operators
15
Logical operators
• Expressions connected by the logical AND (&&) or the
logical OR (||) operator are evaluated left to right and
evaluation stops as soon as truthfulness or falsehood
of the expression is determined. Thus in an
expression:
• E1&&E2, where E1 and E2 are sub-expressions, E1 is
evaluated first. If E1 evaluates to 0, E2 will not be
evaluated and the result of the overall expression will
be 0. If E1 evaluates to a non zero value, then E2 will
be evaluated to determine the truth value of the
overall expression.
16
Example Program
#include <stdio.h>
int main()
{
int i=0, j=1,n;
n=i&&j;
printf("resultant values after evaluation are:");
printf("%d %d %d ",i,j,n);
return 0;
}
19
Logical operators
• E1||E2, where E1 and E2 are sub-expressions, E1 is
evaluated first.
• If E1 evaluates to a non zero value, E2 will not be
evaluated and the result of the overall expression will
be 1.
• If E1 evaluates to 0, then E2 will be evaluated to
determine the truth value of the overall expression.
23
Example Program
#include <stdio.h>
int main()
{
int i=0, j=1, n;
n=i||j;
printf("resultant values after evaluation are:");
printf("%d %d %d ",i,j,n);
return 0;
}
26
Assignment operators
27
Increment & Decrement operators
28
Example Program
#include <stdio.h>
int main()
{
int i=2,j=2,k,n;
k=++i;
n=j++;
printf("i=%d, j=%d, k=%d, n=%d",i,j,k,n);
return 0;
}
30
Example Program
#include <stdio.h>
int main()
{
int i=2,j=2,k,n;
k=--i;
n=j--;
printf("i=%d, j=%d, k=%d, n=%d",i,j,k,n);
return 0;
}
31
Bitwise operators
32
Miscellaneous operator
• Function call operator ()
• Array subscript operator []
• Member select operator i) . ii) ->
• Indirection operator *
• Conditional operator
• Comma operator ,
• Sizeof operator
• Address of operator &
37
Conditional operator
38
Example Program
#include <stdio.h>
int main()
{
int age=19;
(age>=18)? (printf("eligible for voting")) : (printf("not
eligible for voting"));
return 0;
}
40
sizeof() operator
• Sizeof is a much used operator in the C or C++.
• It is a compile time unary operator which can be used to compute the
size of its operand.
Precedence & Associativity of operators
• Certain operators have higher precedence than others;
for example, the multiplication operator has a higher
precedence than the addition operator.
• For example, x = 7 + 3 * 2; here, x is assigned 13, not 20
because operator * has a higher precedence than +, so
it first gets multiplied with 3*2 and then adds into 7.
• Operators Associativity is used when two operators of
same precedence appear in an expression. Associativity
can be either Left to Right or Right to Left.
• For example: ‘*’ and ‘/’ have same precedence and
their associativity is Left to Right, so the expression
“100 / 10 * 10” is treated as “(100 / 10) * 10”.
Example