C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Point Out Errors (Q.No. 7)
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;
}
Error: in case 1*2+4 statement
Error: No default specified
Error: in switch statement
No Error
Answer: Option
Explanation:

Constant expression are accepted in switch

It prints "Case1"

Discussion:
5 comments Page 1 of 1.

Anki said:   1 decade ago
Here i=1 switch terminate on case 1 and print "case1"after that break statment will run. So output is case1.

Khush said:   1 decade ago
Why not case 1*2+4: is a error.

Vikas said:   9 years ago
You are right @Anki.

Nikhil said:   8 years ago
Why not case 1* 2+4: is an error?

Raman said:   7 years ago
There is no error if we initialise i=6 then case2 will be printed.

Post your comments here:

Your comments will be displayed after verification.