C | Loops & Control Structure | Question 13
What will be the output of the following C program segment? (GATE CS 2012)
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") ; } |
chevron_right
filter_none
(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") ;
}
}
Recommended Posts:
- C | Loops & Control Structure | Question 18
- C | Loops & Control Structure | Question 2
- C | Loops & Control Structure | Question 8
- C | Loops & Control Structure | Question 1
- C | Loops & Control Structure | Question 12
- C | Loops & Control Structure | Question 19
- C | Loops & Control Structure | Question 10
- C | Loops & Control Structure | Question 14
- C | Loops & Control Structure | Question 11
- C | Loops & Control Structure | Question 15
- C | Loops & Control Structure | Question 16
- C | Loops & Control Structure | Question 17
- C | Loops & Control Structure | Question 21
- C | Loops & Control Structure | Question 9
- C | Loops & Control Structure | Question 20



