What will be the output of the following C program segment?
char inchar = 'A' ;
switch (inchar)
{
case 'A' :
printf ( "choice A \n" ) ;
case 'B' :
printf ( "choice B " ) ;
case 'C' :
case 'D' :
case 'E' :
default :
printf ( "No Choice" ) ;
}
|
(A)
No choice
(B)
Choice A
(C)
Choice A
Choice B No choice
(D)
Program gives no output as it is erroneous
Answer: (C)
Explanation: There is no break statement in case ‘A’. If a case is executed and it doesn’t contain break, then all the subsequent cases are executed until a break statement is found. That is why everything inside the switch is printed.
Try following program as an exercise.
int main()
{
char inchar = 'A';
switch (inchar)
{
case 'A' :
printf ("choice A \n") ;
case 'B' :
{
printf ("choice B") ;
break;
}
case 'C' :
case 'D' :
case 'E' :
default:
printf ("No Choice") ;
}
}
Quiz of this Question
Level Up Your GATE Prep!
Embark on a transformative journey towards GATE success by choosing
Data Science & AI as your second paper choice with our specialized course. If you find yourself lost in the vast landscape of the GATE syllabus, our program is the compass you need.
Last Updated :
28 Jun, 2021
Like Article
Save Article