Question 6
Back to Exercise
The program below has a bug. Which statement correctly describes it?
#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!