Question 10
Back to Exercise
What is the output of the following C program?
#include <stdio.h>
int main(void) {
int i = 0, n = 0;
while (i++ < 3)
n++;
printf("%d %d\n", n, i);
return 0;
}
- A 3 4 Correct
- B 3 3
- C 4 4
- D 2 3
Correct Answer: Option A
The comparison uses the old value of i and then increments it. Tests 0, 1, 2 pass (n = 3); the test 3 < 3 fails but still increments i to 4.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!