All mock tests are FREE till 31 December — register to save your progress

What is the output of the following program?
#include<stdio.h>
int main() {
  int i = 3;
  while (i--) {
    int i = 10;
    i--;
    printf("%d", i);
  }
  printf("%d", i);
}

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: C. 999-1

Explanation

The outer i (3, 2, 1) makes the loop run 3 times. Each time a new inner i = 10 is decremented and 9 is printed, giving 999. The final test i-- takes the outer i from 0 to −1, so the last printf prints −1: 999-1.

More Programming Languages and Computer Graphics questions