The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+16 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
asked in Programming by Veteran (19.6k points)
edited by | 1.1k views

2 Answers

+26 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 


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


answered by Veteran (347k points)
edited by
But in GATE mark was given for all.
@Mithilesh Good eye :) (y)
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


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

@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

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.
ok i got it...default case is for the situation when case does not matches...but it can be executed even if any case matches.
Why '\' after A is not in the output in case of correct output ?
+4 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
answered by Boss (5.5k points)

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

34,170 questions
40,846 answers
39,703 users