Ex - 1: What is the output?
main()
{
int i=5;
printf(“%d”,i=++i ==6);
}
Ans:
1
Explanation:
The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the result. |
Ex - 2: What is the output?
main()
{
int i = 3;
for (;i++=0;) printf(“%d”,i);
}
Ans:
Compiler Error: Lvalue required.
Explanation:
As we know that increment operators return rvalues and hence it cannot appear on the left hand side of an assignment operation.
|
Ex - 3: What is the output?
main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Ans:
10 10
Explanation:
The Ternary operator ( ? : ) is equivalent for if-then-else statement. So the question can be written as:
if(i,j)
{
if(i,j)
j = i;
else
j = j;
}
else
j = j; |