Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q14 of 15
C - onditional Statements
Find the error in this code snippet:
int num = 3;
switch (num) {
  case 1:
    printf("One\n");
  case 2:
    printf("Two\n");
    break;
  default:
    printf("Default\n");
}
ACase labels must be strings, not numbers.
BSwitch variable must be a character, not int.
CMissing break after case 1 causes fall-through.
DDefault case must be last, but it is not.
Step-by-Step Solution
Solution:
  1. Step 1: Check breaks after cases

    Case 1 lacks a break, so if num is 1, it will fall through to case 2 and print both lines.
  2. Step 2: Validate switch rules

    Switch can use int variables, default can be anywhere, and case labels are numbers here, which is correct.
  3. Final Answer:

    Missing break after case 1 causes fall-through. -> Option C
  4. Quick Check:

    Always add break to avoid fall-through [OK]
Quick Trick: Add break after each case to prevent fall-through [OK]
Common Mistakes:
  • Thinking switch only works with chars
  • Believing default must be last
  • Confusing case label types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes