ProgrammingVery hardProduct company OA · GATE CS

Compiled as C99 or later, what is printed?

Compiled as C99 or later, what is printed?
int i = 1;
for (int i = 5; i < 7; i++) {
    int i = 10;
    printf("%d ", i);
}
printf("%d", i);
  1. A. 10 10 1
  2. B. 5 6 1
  3. C. Compilation error: i is redeclared
  4. D. 10 10 7

Correct answer: A. 10 10 1

In C the for statement is a block and its body is a separate, nested block, so the body may declare its own i, which hides the loop counter. The loop still runs twice (i = 5, 6) and prints the inner 10 each time; the outer i is untouched. C++ rejects the redeclaration, which is the trap.

More C Programming questions

Exams that ask this topic