GATE Computer ScienceVery hardGATE CS (PYQ pattern)

int x = 10; void f() { static int x = 1; x += 2; { int x = 100; x++; } printf("%d ", x); } int main() { f(); f(); { extern int x; printf("%d ", x); } for (int x = 0; x < 2; x++) f(); printf("%d", x); } What is printed?

  1. A. 3 5 10 7 9 2
  2. B. 3 5 103 7 9 10
  3. C. 3 3 10 5 7 10
  4. D. 3 5 10 7 9 10

Correct answer: D. 3 5 10 7 9 10

f's static x is separate from the global: it goes 3, 5, then 7 and 9 in the loop (4 calls total). The inner block's x is local. The extern block sees the global 10, and the loop variable x is gone after the for, so the last print is the global 10.

More Programming and Data Structures (GATE) questions

Exams that ask this topic