C Programming - Control Instructions - Discussion

7. 

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: Option D

Explanation:

Constant expression are accepted in switch

It prints "Case1"


Anki said: (Mar 16, 2011)  
Here i=1 switch terminate on case 1 and print "case1"after that break statment will run. So output is case1.

Khush said: (Aug 4, 2015)  
Why not case 1*2+4: is a error.

Vikas said: (Jul 11, 2016)  
You are right @Anki.

Nikhil said: (Apr 6, 2017)  
Why not case 1* 2+4: is an error?

Raman said: (May 16, 2018)  
There is no error if we initialise i=6 then case2 will be printed.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.