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

What will be the output of the following program?
#include<stdio.h>
int main()
{ int i;
for (i = 0; i++<10; )
printf("%d", i++);
return 0;
}

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

Show answer & explanation

Correct answer: C. 1 3 5 7 9

Explanation

The for-loop has no explicit update clause; both increments happen via the condition (i++<10) and the body (printf with i++). Starting at i=0: the condition test increments i to 1 (test used old value 0, true), the body prints 1 then increments i to 2. Next condition test uses i=2 (true), increments to 3; body prints 3, increments to 4. This pattern continues, printing 1, 3, 5, 7, 9, after which the condition test uses i=10 (10<10 is false) and the loop ends. So the output is 1 3 5 7 9, option (3).

More Programming Languages and Computer Graphics questions