C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Point Out Errors (Q.No. 3)
3.
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        printf("This is c program.");
        case 1:
            printf("Case1");
            break;
        case 2:
            printf("Case2");
            break;
    }
return 0;
}
Error: No default specified
Error: Invalid printf statement after switch statement
No Error and prints "Case1"
None of above
Answer: Option
Explanation:

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".

Discussion:
8 comments Page 1 of 1.

Rama said:   1 decade ago
What is the use of int main () and return 0? Can't we write main () without any return?

Jay said:   1 decade ago
Warning shall be there: unreachable code(for printf statement)....(according to turbo C++ compiler).

Aman said:   1 decade ago
@Rama:

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:   9 years ago
But here, there are two cases so don't anyone think I should be initialized with 2!?

Nijara said:   9 years ago
Why is the printf ignored by the compiler?
(1)

Swapnil Bais said:   9 years ago
@Nijara.

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

SwapnilBais said:   9 years ago
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.
(2)

Rashmi said:   5 years ago
Switch statement does not print any statement outside case and default blocks.
(1)

Post your comments here:

Your comments will be displayed after verification.