LearnFrenzy
Sign in Take a free test

Control Instructions · Find the Error · Question 4

Bug: missing break makes extra cases run Answer, explanation & discussion

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

The status code is 404. What does this program print, and why?

main.c
#include <stdio.h>

int main(void) {
    int code = 404;
    switch (code) {
    case 200: puts("OK");
    case 404: puts("Not Found");
    case 500: puts("Server Error");
              break;
    default:  puts("Unknown");
    }
    return 0;
}
  • A Not Found
  • B Not Found and Server Error – case 404 has no break, so it falls through into case 500 Correct
  • C OK, Not Found and Server Error
  • D Unknown
Correct Answer: Option B

Entry is at case 404; without a break, control continues into case 500 until its break. Add break; after every case (C23: mark deliberate fall-through with [[fallthrough]]).

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