What is printed by this C code: int n = 2; switch (n) { case 1: printf(“A”); case 2: printf(“B”); case 3: printf(“C”); break; default: printf(“D”); }?
Correct answer B. BC
Explanation
switch jumps to the matching label, case 2, and execution then falls through every following statement until a break or the end of the switch. So it prints B, falls into case 3 and prints C, and the break stops it before default. Answer B would be correct only if case 2 ended with break. Missing breaks are a common bug, which is why compilers offer -Wimplicit-fallthrough.
Answer checked How we write and check questions
Discussion