GATE 2012 COMPUTER SCIENCE & INFORMATION TECH. – CS – C Programming | Q.3

📝 GATE 2012 – C Programming – Question 3
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
💡 This is a classic switch-case fall-through question. The important point is that there are no break statements between the cases.
💻 Given Code
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");
}
🔎 Key Point: When inChar = ‘A’, execution starts at case ‘A’. Since there is no break, execution continues into the following cases. This is called fall-through.
🔹 Step 1: inChar is ‘A’
Therefore, execution starts at:
case 'A' :
    printf("Choice A\n");
📤 Output so far:
Choice A
💡 The statement prints Choice A. However, there is no break statement after this case, so execution will continue to the next case.
🔹 Step 2: Is there a break statement?
No.
Therefore, execution falls through to the next cases.
case 'B' :

case 'C' :
    printf("Choice B");
📤 Output becomes:
Choice A
Choice B
💡 Remember: Without a break, C does not stop execution after case ‘A’. It continues executing the statements in the following cases.
🔹 Step 3: Continue falling through
There is still no break.
Therefore, execution continues and reaches:
default :
    printf(" No Choice");
📤 Output becomes:
Choice A
Choice B No Choice
💡 Why? Once execution enters case ‘A’, it keeps moving downward because there are no break statements. It finally executes the default statement too.
🎯 Final Output
Choice A
Choice B No Choice
✓ Correct Answer: (C)
💡 Easy Rule to Remember
In a switch statement:
case X:
    statements;
    break;
  • If break is present → the switch statement ends.
  • If break is absent → execution continues into the following cases.
🧩 Example
switch(x)
{
    case 1:
        printf("A");

    case 2:
        printf("B");

    case 3:
        printf("C");
}
📤 Output:
ABC
🔎 This happens because there are no break statements between the cases.
🎯 Applying the Rule to This Question
  • case ‘A’ prints “Choice A”.
  • There is no break, so execution falls through to the next cases and prints “Choice B”.
  • Execution continues further and reaches default, which prints ” No Choice”.
✅ Hence, the correct answer is Option (C).

switch Expression in C

In C, the expression written inside the parentheses of a switch() statement is called the switch expression.

switch (expression)
{
    case constant:
        statements;
}

The value produced by the switch expression is compared with the values specified in the case labels.

1. What data types are allowed in switch()?

In C, the switch expression must produce an integer value. Therefore, integer types such as the following can be used:

  • char
  • short
  • int
  • long
  • enum

The most commonly used type is int, but char and enum are also frequently used with switch statements.

2. Example using int

int choice = 2;

switch (choice)
{
    case 1:
        printf(“One”);
        break;

    case 2:
        printf(“Two”);
        break;
}

Here, choice is an int, so it can be used as the switch expression.

3. Example using char

char ch = ‘A’;

switch (ch)
{
    case ‘A’:
        printf(“Choice A”);
        break;

    case ‘B’:
        printf(“Choice B”);
        break;
}

A char can be used because a character is represented internally by an integer character code.

Example:
'A' has an integer character code (65 in ASCII), 'B' has 66, and so on.

4. Data types not allowed in switch()

Floating-point and string values cannot be used as the switch expression in C.

Data Type Allowed? Example
int ✓ Yes switch(x)
char ✓ Yes switch(ch)
short / long ✓ Yes switch(n)
enum ✓ Yes switch(color)
float ✗ No switch(2.5)
double ✗ No switch(3.14)
string ✗ No switch(str)

5. Why can char be used?

In C, a character is stored as an integer character code. For example, using the commonly used ASCII encoding:

‘A’ = 65    ‘B’ = 66    ‘C’ = 67

Therefore, a statement such as:

char ch = ‘A’;
switch(ch)

can be used because the value of ch is an integer character code.

6. What about case labels?

The values written after case must be constant expressions that are compatible with the switch expression.

For example:

int x = 2;

switch(x)
{
    case 1:
        printf(“One”);
        break;

    case 2:
        printf(“Two”);
        break;
}

Here, 1 and 2 are constant integer values, so they can be used as case labels.

Important: A normal variable cannot be directly used as a case label. For example:

int a = 1;
case a:   /* Not valid */

7. Complete Example

#include <stdio.h>

int main()
{
    char inChar = ‘A’;

    switch(inChar)
    {
        case ‘A’:
            printf(“Choice A”);
            break;

        case ‘B’:
            printf(“Choice B”);
            break;

        default:
            printf(“No Choice”);
    }

    return 0;
}

Since inChar contains 'A', the program executes the statements associated with case 'A'.

Exam Tip:
Remember the simple rule:

switch expression → integer value

So char, int, short, long, and enum can be used, while float, double, and strings cannot be used directly as the switch expression.

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »