ProgrammingModerateTCS NQT · Wipro

What is printed?

What is printed?
int x = 5;
x = x << 2 >> 1;
printf("%d", x);
  1. A. 10
  2. B. 40
  3. C. 20
  4. D. 2

Correct answer: A. 10

Shift operators group left to right: (5 << 2) >> 1 = 20 >> 1 = 10. Reading it as 5 << (2 >> 1) gives 10 too by coincidence, but '20' forgets the right shift and '2' shifts in the wrong order.

More C Programming questions

Exams that ask this topic