ProgrammingModerateWipro · Capgemini pattern

What is printed?

What is printed?
int x = 1;
{
    int x = 2;
    { x = 3; }
    printf("%d ", x);
}
printf("%d", x);
  1. A. 3 1
  2. B. 2 3
  3. C. 3 3
  4. D. 2 1

Correct answer: A. 3 1

The inner block declares a new x that hides the outer one. The innermost block declares nothing, so x = 3 assigns to the nearest x, the inner one, which prints 3. The outer x was never touched and prints 1.

More C Programming questions

Exams that ask this topic