ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int i = 0;
int arr[5] = {10, 20, 30, 40, 50};
while (i < 5)
    printf("%d ", arr[i++]);
printf("%d", i);
  1. A. 10 20 30 40 50 5
  2. B. 10 20 30 40 5
  3. C. 10 20 30 40 50 4
  4. D. Undefined behaviour

Correct answer: A. 10 20 30 40 50 5

Each loop iteration is its own statement, so there's no unsequenced conflict: arr[i++] fully evaluates (reading the current i, then incrementing it) before the next iteration begins. The loop prints all 5 elements in order (10 20 30 40 50), and by the time it exits, i has been incremented 5 times to 5, which the final printf reports.

More C Programming questions

Exams that ask this topic