Machine-translated by Google. Questions are authored in English.
Top

Discussion

Point out the error, if any in the program.

#include <stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        case 1:
           printf("Case1");
           break;
        case 1*2+4:
           printf("Case2");
           break;
    }
return 0;
}

 

  • A.Error: in case 1*2+4 statement
  • B.Error: No default specified
  • C.Error: in switch statement
  • D.No Error

Answer: D

Correct answer: No Error

case 1*2+4 is a constant expression, which the compiler evaluates to 6 - that is allowed, and 6 does not clash with case 1. A default label is optional. So the program compiles and, with i equal to 1, prints Case1.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.