C | Operators | Question 16

Predict the output of the below program:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    printf("%d", 1 << 2 + 3 << 4);
    return 0;
}

chevron_right


(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



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.