LearnFrenzy
Sign in Take a free test

C Const · Find the Error · Question 6

Error: a const int used as a case label Answer, explanation & discussion

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

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

main.c
#include <stdio.h>

int main(void) {
    const int QUIT = 4;
    int choice = 4;
    switch (choice) {
    case QUIT:
        puts("bye");
        break;
    default:
        puts("again");
    }
    return 0;
}
  • A switch needs a char
  • B A case label must be an integer constant expression; a const int is not one in C Correct
  • C QUIT must be global
  • D There is no bug
Correct Answer: Option B

Use an enum (enum { QUIT = 4 };) – the idiomatic C way to name menu choices – or C23 constexpr int QUIT = 4;.

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