444 views

What will be output printed by the following program?

#include<stdio.h>
main()
{
int c=4;
switch(c)
{
c=c-1;

case 4:
printf("IITB ");
break;
default:
printf("IISc ");
case 3:
printf("IITM ");
case 2 :
printf("IITD");
}
}

1. $\text{IITB IITM}$
2. $\text{IITB IISc IITM}$
3. $\text{IITB}$
4. $\text{IITB IITM IITD}$

### 2 Comments

Option C i.e IITB will be the answer
edited
yes

## 3 Answers

$c=c-1;$

This statement is outside all cases (including default case) hence it will not get executed.

There is a break statement in case $4$ hence only case $4$ gets executed.

Here switch case is executed based on the value provided by $c=4$ in the given program. Now controls come to switch(4). Based on the value it will execute the statements associated with it. one more point here is the break keyword, this keyword is used to stop the execution of the program inside the switch block.

1. Option (A) is wrong here because after printing IITB there is a break so IITM will not be printed.
2. Option (B) is wrong because the default case is only executed if no case is matching but here switch(4) will print IITB but after that, there is a break so IISc will not be printed.
3. switch(4) is match and print IITB as output.
4. Option (D) is also wrong after IITB IITM can not be printed.

So Option (C) is correct.

After getting value of c as 4 control will directly jumps to Case 4: and print result as IITB..

Answer:

5 votes
2 answers
1
4 votes
2 answers
2
4 votes
1 answer
3
5 votes
1 answer
4