Aspire's Library

A Place for Latest Exam wise Questions, Videos, Previous Year Papers,
Study Stuff for MCA Examinations - NIMCET

Previous Year Question (PYQs)



 What is the output of the following C program?

int main()
{
    char ch = 'A';
    int x = 97;
    int y = sizeof(++x);
    printf("\nx is %d", x);
    while (ch <= 'F')
    {
        switch (ch)
        {
            case 'A':
            case 'B':
            case 'C':
            case 'D': ch++; break;
            case 'E':
            case 'F': ch++;
        }
        putchar(ch);
    }
    return 0;
}






Solution

Solution:

1️⃣ `int y = sizeof(++x);`  
→ `sizeof` is a compile-time operator, so `++x` is **not evaluated**.  
Hence, `x` remains unchanged (`x = 97`).

2️⃣ `printf("\nx is %d", x);`  
→ prints: `x is 97`

3️⃣ Loop execution:

| ch  | switch(ch) executes | putchar(ch) prints | next value of ch |
|------|----------------------|--------------------|------------------|
| 'A'  | falls to case 'D' → ch++ → break | prints `'B'` | 'B' |
| 'B'  | falls to case 'D' → ch++ → break | prints `'C'` | 'C' |
| 'C'  | falls to case 'D' → ch++ → break | prints `'D'` | 'D' |
| 'D'  | case 'D' → ch++ → break | prints `'E'` | 'E' |
| 'E'  | case 'E' → fall through to case 'F' → ch++ | prints `'F'` | 'F' |
| 'F'  | case 'F' → ch++ | prints `'G'` | 'G' (loop ends) |

Output sequence = `x is 97 BCDEFG`





Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More


Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More

Ask Your Question or Put Your Review.

loading...