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 1 of 2.

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

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.

Hari said:   1 decade ago
In default there is no break statement.

When there is no break statement it does not check for any condition (case) but it prints the statement.

Archie said:   1 decade ago
I ran this program in Ubuntu and I got only the print statement in default as output.

Govind said:   1 decade ago
@Archie.

I don't think that is possible. Because I did run the same program and the output is as it is said to be. And according to the Switch statement rules this is the right output.

Thanks.

Biswajit said:   1 decade ago
In switch statement, if any one case is not satisfied then default statement is encountered.

Sheetal said:   1 decade ago
Here if i is initialized with any 1, 2 or 3 then we will get output printed as this is case 1, 2 or 3 respectively. i.e. its not seeing the default case and just switching to the case where its satisfying. And if there is any value of i that's not in the case then default as well as case 1 statement thing is printed and then its breaking out!

Snehal said:   1 decade ago
If there is statement like i=2 then what will be the output?

Ankita said:   1 decade ago
If i=2 then output would be "This case 2".

AdaLove said:   1 decade ago
There are two central ideas to the problem:

1. The default case will be executed only when there is no matching case. The position of the default case does not matter; whether you place it in the end or in between or make it the very first case.

2. If a case is executed and there is no break statement, the subsequent cases are executed without checking the case values util a break is encountered or all cases are executed.

Thus here, default is executed and then case 1 is executed and then execution breaks out of the switch case.


Post your comments here:

Your comments will be displayed after verification.