C Programming - Control Instructions - Discussion

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

switch(i) has the variable i it has the value '1'(one).

Then case 1: statements got executed. so, it prints "Hi". The break; statement make the program to be exited from switch-case statement.

switch-case do not execute any statements outside these blocks case and default

Hence the output is "Hi".

Discussion:
5 comments Page 1 of 1.

Rashmi B Nanadi said:   6 months ago
@All.

The switch case does not execute any statements outside these blocks (case and default).
Statements are written inside the switch, but before any case, DO execute.

Gunjan Agarwal said:   6 years ago
#include<stdio.h>
int main()
{
switch(1)
{

int b=10;
case 1: printf ("%d",b);

}
return 0;

}

case 1: printf ("%d",b);

It will not execute the statement as because it is unable to find out b variable value as because switch only executes case and default statements.

Pooja said:   8 years ago
Switch(1)
{
.
.
.
int b=10
case : printf ("%d",b);
.
.
.
}


Will it work?

Can anyone explain me?

Jay said:   1 decade ago
Option c is correct because in switch only label are executes like case or default if u run this program in turboc3 it givs warring unreachable code Printf("Hello\n") means it not going to execute...
(1)

Sanchit said:   1 decade ago
Why printf ("Hello\n") ; is not execute?

Why option a is not correct?

Post your comments here:

Your comments will be displayed after verification.