in Programming edited by
5,989 views
28 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 edited by
by
6k views

Subscribe to GO Classes for GATE CSE 2022

4 Answers

36 votes
 
Best answer

There is a `space` in between the `\` and `n`.  (see-Q-no.-3)

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

So, output of the given program is:

Choice A nChoice BNo Choice 

Which includes:

'n'

And there is no new line or spaces between outputs. Hence, there is no option matching.

http://stackoverflow.com/questions/33694700/im-missing-something

edited by
by

16 Comments

But in GATE mark was given for all.
12
@Mithilesh Good eye :) (y)
1
I believe output should be
Choice A

NO Choice

since inChar, 'A' is not equal to case constant, 'C' present in switch expression , "Choice B" wouldn't get printed

 

Pls clarify

Note : Marks were given for all in answer key of Gate exam
1
0

Arjun sir WHAT WE DO IN GATE EXAM IF THERE IS NO OPTION MATCHED.....????? 

0
@Arjun Suresh Sir, If there were no space between / and n, then will option C be correct answer?
0
yes.
0
edited by

@srestha..... no,even if If there were no space between / and n, then also option c would not be correct answer.

@Arjun sir.. pls.verify this

correct ans would have been

Choice A

Choice B

i have taken this pic from dennis ritchie

2
That statement is correct -- default will execute if no other case matches. But it does not say that default won't be executed if some other case matches.

Actually when some case matches all statements which follows are executed ignoring any labels. We have to explicitly use "break" to avoid this.
19
edited by
ok sir....now i got it...default case is for the situation when case does not matches...but it can be executed even if any case matches.
0
Why '\' after A is not in the output in case of correct output ?
3
@Arjun sir pls correct question ,there is no space between this progam
0

Why '\' after A is not in the output in case of correct output ?

??

0

@jatin khachane 1 "\" followed by something are predefined.

\ is a non printable character. If you want to print it, you have to do "\\".

Here "\ " has no meaning.

4
Yes this condition is known as fall through

@ArjunSir
0
19 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
3 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
1 vote

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

Ask
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true