in Programming in C
405 views
3 votes
3 votes

What will be the output of the following C program fragment?

int n =1;
switch(n)
{
    case 1: printf("One");
    case 2: printf("Two");
    case 3:
    case 4:
    case 5:
    default: printf("Wrong Choice");
}

A) One

B) One Two Wrong Choice

C) Two

D) One Two

in Programming in C
405 views

1 comment

One Two Wrong choice

Until it gets break statement after switch case, it keep on printing
0
0

2 Answers

3 votes
3 votes
Best answer
Option B: One Two Wrong Choice

Since no break statement, all cases from matched case will execute
selected by
1 vote
1 vote
The condition of a switch statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from falling through and executing the code in all the other case statements. An important thing to note about the switch statement is that the case values may only be constant integral expressions.
so
(B) is the right choice

Related questions