C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 17)
17.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=4;
    switch(i)
    {
        default:
           printf("This is default\n");
        case 1:
           printf("This is case 1\n");
           break;
        case 2:
           printf("This is case 2\n");
           break;
        case 3:
           printf("This is case 3\n");
    }
    return 0;
}
This is default
This is case 1
This is case 3
This is default
This is case 1
This is case 3
This is default
Answer: Option
Explanation:

In the very begining of switch-case statement default statement is encountered. So, it prints "This is default".

In default statement there is no break; statement is included. So it prints the case 1 statements. "This is case 1".

Then the break; statement is encountered. Hence the program exits from the switch-case block.

Discussion:
12 comments Page 2 of 2.

Shiwam pandey said:   1 decade ago
@Ansi

I think that value 4 is comsder as true value since ti is not a
case in switch and true value should be consider as 1.

Hence case 1 is printed. That's it.

Ansi said:   1 decade ago
but how is this possible value of i is 4 then how case 1 is printed


Post your comments here:

Your comments will be displayed after verification.