Check The Programming Section
What will be the output of the following code?
int main(){
int i = 3, k;
k = i++;//line2
printf(“%d %d”, i, k);
}
3 3
4 4
4 3
3 4
Answer: In line2 the post increment operator is used. So the value of the operand i (3) will be assigned to k and then the value of i get incremented by 1. The output will be 4 and 3.
What will be the output of the following program?
int main(){
int i = 2;
printf(“%d %d”,++i, ++i);
}
3 4
4 3
4 4
5 5
Answer: The behaviour is undefined to the compiler. The expression used in printf statement starts evaluation from right to left and both the pre increment operation gets evaluated first and the value of i is incremented two times to 5. At last printf() function prints the output as 5 5.
What will be the output of the following program?
int main(){
int x = 10, y = 20, z = 5, i;
i = x < y < z;//line2
printf(“%d”, i);
}
1
0
Error
None of the above
Answer: At line2 evaluation starts from left to right. First the value of x gets compared with y and returns 1, because x < y. Then the return value of x<y gets compared with the value of z as 1 < 5 and returns 1 as true.
What will be the output of the code?
int main(){
int i = -3, j = 2, k = 0, m;
m = ++i && ++j || ++k;//line2
printf(“%d %d %d %d”, i, j, k, m);
}
-3 2 0 1
-2 2 1 0
-2 3 0 1
-2 3 0 0
Answer: The expression at line2 will be evaluated as (((++i) && (++j)) || ++k). At first the value of i gets incremented to -2. In C/C++, any value other than 0 is considered as a true value. So the LHS operand is considered as true and the RHS operand (++j) of logical AND gets incremented the value of j by 1 to 3 (consider as TRUE). So the expression (++i && ++j) returns a as true. At last the expression (1 || ++k) returns 1, but the ++k will not be evaluated. Because once LHS operand of logical OR represents a true value then RHS operand (++k) of OR will not be considered. The final output will be -2 3 0 1.
What will be the output of the code?
int main(){
int = -3, j = 2, k = 0, m;
m = ++j && ++i || ++k;//line2
printf(“%d %d %d %d”, i, j, k, m);
}
-3 2 0 1
-2 2 1 0
-2 3 0 1
-2 3 0 0
Answer: The expression at line2 gets executed the same way as Q. No. 4. Here the only difference is the operand of logical AND operator. So due to the post increment operator and the initial value of i and j, this code will produce the same output as Q. No. 4. The order evaluation is discussed in Q. NO. 4.
What will be the output of the code?
int main(){
int = -3, j = 2, k = 0, m;
m = ++i || ++j && ++k;//line2
printf(“%d %d %d %d”, i, j, k, m);
}
-2 2 0 1
-2 2 1 0
-2 3 0 1
-2 3 0 0
Answer: The expression at line2 will be evaluated as ((++i) || ++j && ++k). At first the unary operation ++i gets evaluated and the value of i gets incremented to -2. So the LHS operand returns 1 as the outcome of the expression and assigns it to m. So the value of j and k remain unchanged and the output is -2 2 0 1.
What will be the output of the code?
int main(){
int = -3, j = 2, k = 0, m;
m = ++i && ++j && ++k;//line2
printf(“%d %d %d %d”, i, j, k, m);
}
-3 2 0 1
-2 2 1 0
-2 3 1 1
-2 3 1 0
Answer:The expression at line2 will be evaluated from left to right and gets evaluated as ((++i && ++j) && ++k). The value of i and j gets incremented by 1 to -2 and 3 and the LHS operand (++i && ++j) of && returns 1 as true value. So the RHS operand (++k) of && gets evaluated to 1 and the outcome of the expression at line2 returns 1 and assigns it to m. The output will be -2 3 1 1.