in Programming in C retagged by
10,843 views
40 votes
40 votes

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

 char inChar = 'A';
    switch ( inChar ) {
       case 'A' : printf ("Choice A \ n");
       case 'B' :
       case 'C' : printf ("Choice B");
       case 'D' :
       case 'E' :
       default : printf ("No Choice"); 
    }
  1. No Choice
  2. Choice A
  3. Choice A
    Choice B No Choice
  4. Program gives no output as it is erroneous
in Programming in C retagged by
by
10.8k views

4 Comments

you have read right . It is correct
0
0

@Rajat Agrawal007 To be more precise it should yield an integer value, like if its switch (7.0) then also compiler will throw error. So the concept is it must be an integer.

0
0

Correct question would be

char inChar = 'A';
    switch ( inChar ) {
       case 'A' : printf ("Choice A \n");
       case 'B' :
       case 'C' : printf ("Choice B ");
       case 'D' :
       case 'E' :
       default : printf ("No Choice");
    }
0
0

5 Answers

45 votes
45 votes
Best answer

There is a `space` in between the `\` and `n` in the printf of case ‘A’ and also no space after the printf for case ‘C’ in the original question (see-Q-no.-3). Due to this “Marks to All” was done.

Lets assume no space for “\n” in the printf for case ‘A’ and there is a space at the end of printf of case ‘C’.

When a case of a switch is matched, execution continues from the matched case until a “break” is encountered (any intermediate case labels simply gets ignored).

So, output of the given program is:

Choice A
Choice B No Choice 

Option C.

edited by
by

4 Comments

\’ is used for line continuation or line extension.

Look below two programs. First gives error while second wouldn’t.

Program -1 :

 

Program -2 :

Notice one thing, we broke “Hello world” token in two lines. :)

4
4
edited by

If the q had 

  case 'A' : printf ("Choice A \n");

Then the Output would be :-

Choice A 
Choice BNo Choice

Even this is not present in Option C.

Edit: Ok, there is a space after Choice B. So in new question, ans is Option C.

Thanks @Pranavpurkar for pointing it out. 

1
1

@Abhrajyoti00

are you considering the space between (as it  is not the same in options)

  1. Choice BNo Choice

B and N?

 

1
1
21 votes
21 votes
There is no break statement in  any case . If a case 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.

so ans is C
4 votes
4 votes

Syntax:

  1. switch(expression)
  2. {
  3. case constant-exp1 : statements; break;
  4. case constant-exp2: statements; break;
  5. default: statements;
  6. }

In switch case, the break statement is used to terminate the switch case. Basically it is used to execute the statements of a single case statement. If no break appears, the flow of control will fall through all the subsequent cases until a break is reached or the closing curly brace ‘}’ is reached.

In above , switch case don't have break statement , 

1. If inChar='A'  , flow control will execute all statements after Case A: till switch case end (in case any break states occurs between statement , it will terminated at the point but in above example we don't have any break statement, so whole switch block will execute).

Output : Choice A
               Choice B No Choice

In case if inChar='C' then output will be  Choice B No Choice.

it's not necessary that we should always add break statement for each cases in switch, including break statement in switch case is based on our requirement.

Answer is C

edited by
2 votes
2 votes

Answer: Option C

There is no break statement after case A, if there is no break statement then it will execute all the subsequent cases. So it will execute all those print statements. Hence option C. 

To know more about Switch statement refer this: https://www.geeksforgeeks.org/interesting-facts-about-switch-statement-in-c/ it will clear all the doubts.

 

Answer:

Related questions