in Programming edited by
444 views
4 votes
4 votes

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}$
in Programming edited by
444 views

2 Comments

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

3 Answers

5 votes
5 votes
$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.
1 vote
1 vote

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.

Ref: switch Statement (C)

0 votes
0 votes

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

Answer:

Related questions