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

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;
}

A.

0 1 2 3 4 5 6 7 8 9

B.

1 2 3 4 5 6 7 8

C.

1 3 5 7 9

D.

2 4 6 8

Correct option is C


The program uses the post-increment operator (i++) both in the for loop condition and inside the printf() statement. Initially, i = 0. In the first condition, i++ < 10 compares 0 with 10 and then increments i to 1; therefore, printf("%d", i++) prints 1 and increments i to 2. The same process repeats, causing the values 1, 3, 5, 7 and 9 to be printed. After printing 9, i becomes 10, and the next condition 10 < 10 becomes false. Hence, the output is 13579.
Information Booster
1. Post-increment (i++)
· The post-increment operator first uses the current value of the variable and then increments it by 1.
· For example:
int i = 5;
printf("%d", i++);
prints 5, after which i becomes 6.
2. Evaluation of the for condition
· The condition is:
i++ < 10
· The comparison uses the old value of i.
· After the comparison, i is incremented.
· Thus, every successful condition check increases i by 1 before entering the loop body.
3. Evaluation inside printf()
· The statement:
printf("%d", i++);
prints the current value and then increments i.
· Therefore, the value of i increases twice per successful iteration: once in the condition and once in the loop body.
4. Iteration-wise execution

Iteration
Value tested in condition
i after condition
Value printed
i after printf()
1
0
1
1
2
2
2
3
3
4
3
4
5
5
6
4
6
7
7
8
5
8
9
9
10
6
10
11


5. Final output
· Since the printf() statement contains no spaces or newline:
13579
· Conceptually, the printed sequence is 1, 3, 5, 7, 9.
Additional Knowledge
· Important C-language point: In this program, the two i++ operations occur in separate full expressions—the for condition and the printf statement—so there is no unsequenced modification issue between these two increments.
· When you see i++ in both the for condition and the loop body, trace the value after each increment separately rather than assuming the loop variable increases only once per iteration.

Free Tests

Free
Must Attempt

Basics of Education: Pedagogy, Andragogy, and Hutagogy

languageIcon English
  • pdpQsnIcon10 Questions
  • pdpsheetsIcon20 Marks
  • timerIcon12 Mins
languageIcon English
Free
Must Attempt

UGC NET Paper 1 Mock Test 1

languageIcon English
  • pdpQsnIcon50 Questions
  • pdpsheetsIcon100 Marks
  • timerIcon60 Mins
languageIcon English
Free
Must Attempt

Basics of Education: Pedagogy, Andragogy, and Hutagogy

languageIcon English
  • pdpQsnIcon10 Questions
  • pdpsheetsIcon20 Marks
  • timerIcon12 Mins
languageIcon English
test-prime-package

Access ‘UGC NET Computer Science’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
528k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow