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

C# Programming Control Instructions Difficulty: Easy
Choose an option
  • A
    Error: No case statement specified
  • B
    Error: No default specified
  • C
    No Error
  • D
    Error: infinite loop occurs
  • E
    None of the above

Answer

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

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