Question 6
Back to Exercise
Compiled as standard C with -pedantic-errors, this program is rejected. Why?
#include <stdio.h>
int main(void) {
int marks = 93;
switch (marks) {
case 90 ... 100: puts("A"); break;
case 75 ... 89: puts("B"); break;
default: puts("C or below");
}
return 0;
}
- A switch cannot use int
- B case 90 ... 100 is a GNU extension (proposed for a future standard), not standard C – use if-else for ranges Correct
- C default must come first
- D There is no bug
Correct Answer: Option B
Standard C case labels are single integer constants. GCC and Clang accept ranges only as an extension; Clang calls it "a C2y extension". An if-else ladder handles ranges portably.
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!