C Programming - Control Instructions - Discussion


Point out the error, if any in the program.

int main()
    int i = 1;
        printf("This is c program.");
        case 1:
        case 2:
return 0;

[A]. Error: No default specified
[B]. Error: Invalid printf statement after switch statement
[C]. No Error and prints "Case1"
[D]. None of above

Answer: Option C


switch(i) becomes switch(1), then the case 1: block is get executed. Hence it prints "Case1".

printf("This is c program."); is ignored by the compiler.

Hence there is no error and prints "Case1".

Rama said: (Sep 9, 2011)  
What is the use of int main () and return 0? Can't we write main () without any return?

Jay said: (Sep 25, 2011)  
Warning shall be there: unreachable code(for printf statement)....(according to turbo C++ compiler).

Aman said: (Dec 1, 2014)  

If we used datatype along with fun name so it is necessary to return something value if you wish to write main with return you can use void infront of main.

Pooja said: (Jul 31, 2016)  
But here, there are two cases so don't anyone think I should be initialized with 2!?

Nijara said: (Aug 22, 2016)  
Why is the printf ignored by the compiler?

Swapnil Bais said: (Feb 15, 2017)  

printf is ignored because the compiler after coming to the switch statement it directly jumps to the case.

Swapnilbais said: (Feb 15, 2017)  
The printf is ignored because the compiler after reaching to the switch statement it directly jumps on the cases which is called by the user.

Rashmi said: (Aug 21, 2020)  
Switch statement does not print any statement outside case and default blocks.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.