Difficulty: Easy
Correct Answer: No Error
Explanation:
Introduction / Context:
This question clarifies whether a switch
statement must contain at least one case
or default
. Understanding the grammar of C control statements helps prevent false alarms about perfectly legal constructs.
Given Data / Assumptions:
switch (a) { }
has an empty compound statement.
Concept / Approach:
In C, a switch
statement requires a controlling expression and a statement body. That body may be any statement, including an empty compound statement. Case labels are optional; without any labels, the switch does nothing and control proceeds to the next statement. There is no inherent loop in a switch, so the idea of an “infinite loop” does not apply.
Step-by-Step Solution:
Enter switch: no labels → no statements executed.Continue to printf
and print the message.Program returns successfully.
Verification / Alternative check:
Compiling under common compilers yields no diagnostic. Adding a default
is optional and would simply execute that block whenever reached.
Why Other Options Are Wrong:
No case/default specified — not required. Infinite loop — a switch does not loop by itself.
Common Pitfalls:
Confusing switch
with loops; believing default is mandatory.
Final Answer:
No Error
Discussion & Comments