Question 7
Back to Exercise
What is the output of the following C program?
#include <stdio.h>
int main(void) {
int x = 0;
int r = x++ || x++;
printf("%d %d\n", r, x);
return 0;
}
- A 1 2 Correct
- B 0 1
- C 1 1
- D 0 2
Correct Answer: Option A
|| has a sequence point after its left operand. The first x++ yields 0 (false) and makes x 1, so the right side runs: it yields 1 (true) and makes x 2. Unlike i = i++, this is well-defined.
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!