Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.