Predict the output of the below program:
#include <stdio.h> int main()
{ printf ( "%d" , 1 << 2 + 3 << 4);
return 0;
} |
(A) 112
(B) 52
(C) 512
(D) 0
Answer: (C)
Explanation: The main logic behind the program is the precedence and associativity of the operators. The addition(+) operator has higher precedence than shift(<<) operator. So, the expression boils down to 1 << (2 + 3) << 4 which in turn reduces to (1 << 5) << 4 as the shift operator has left-to-right associativity.
Quiz of this Question