GATE / CS ARCHIVE

GATE CSE 2012 / QUESTION 3

Switch fall-through and escape sequences

Programming in C · Control Flow & Loop Tracing · Full GATE CSE 2012 paper

GATE 2012, Q3 1 mark · MCQ
Report issue

Control Flow & Loop Tracing

Question 3, GATE 2012: 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” ) ; }
       (A) No Choice
       (B) Choice A
       (C) Choice A
            Choice B No Choice
       (D) Program gives no output as it is erroneous
Tap to enlarge
Your answer
Answer & solution

Official answer: Marks to All

Our answer: C under intended newline interpretation

If the first printf is interpreted as the intended standard newline escape \n, execution enters case A and falls through every following label because there is no break. It prints Choice A, a newline, then Choice B and No Choice, matching C. The printed source visibly separates the backslash and n in the first string; taking backslash-space literally is not the standard newline escape and does not justify an unconditional portable-output answer. The official key awards Marks to All. This is an independently observed printing/interpretation concern, not a claim about the authority’s reason.

Check: Switch labels are entry points, not implicit stopping points. After executing the case-A printf, the empty B label passes through to C’s printf; D and E are also empty, so execution reaches default’s printf before leaving the switch.

Original paper, page 2 · Answer key