LearnFrenzy
Sign in Take a free test

Control Instructions · Find the Error · Question 2

Error: duplicate case value in a switch Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

#define START 1
#define RESUME 1

int main(void) {
    int code = 1;
    switch (code) {
    case START:  puts("start");  break;
    case RESUME: puts("resume"); break;
    }
    return 0;
}
  • A Macros cannot be case labels
  • B START and RESUME both expand to 1, and two case labels may not have the same value Correct
  • C code must be a char
  • D There is no bug
Correct Answer: Option B

After preprocessing both labels are case 1:, a duplicate, which is a compile error. Give the constants distinct values – an enum does that automatically.

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