Question 8
Back to Exercise
Recent compilers reject this program. What is wrong with the condition?
#include <stdio.h>
int main(void) {
int low = 10, x = 50, high = 20;
if (low < x < high)
puts("in range");
else
puts("out of range");
return 0;
}
- A low and high must be constants
- B low < x < high means (low < x) < high, comparing 0 or 1 with high – not a range test; write low < x && x < high Correct
- C The variables must be unsigned
- D There is no bug; it prints out of range
Correct Answer: Option B
The first comparison gives 1, and 1 < 20 is true, so the old behaviour was "in range" for 50. GCC warns about this, and Clang 21 makes it an error by default because it never means what it looks like.
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!