C switch with empty body:\nDoes the following program contain any error when a switch has no case/default blocks?\n\n#include <stdio.h>\nint main()\n{\n int a = 10;\n switch (a)\n {\n }\n printf("This is c program.");\n return 0;\n}

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.
  • The program then prints a message and returns.


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

More Questions from Control Instructions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion