LearnFrenzy
Sign in Take a free test

Decision Making · Find the Error · Question 6

Error: case ranges in a switch (not standard C) Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

Compiled as standard C with -pedantic-errors, this program is rejected. Why?

main.c
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links