Question 2
Back to Exercise
The program below has a bug. Which statement correctly describes it?
#include <stdio.h>
int main(void) {
int i = 1;
printf("%d %d\n", i++, i++);
return 0;
}
- A printf cannot take expressions
- B Both arguments modify i, and argument evaluation is unsequenced – undefined behaviour Correct
- C i must be initialised to 0
- D There is no bug; it always prints 1 2
Correct Answer: Option B
The order in which arguments are evaluated is unspecified, and two unsequenced modifications of the same object are undefined. Compute the values in separate statements first.
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!