GateAI
Programming in C

GATE CSE 2012 · Q48

MCQ2 marksMedium

Time

Common Data for Questions 48 and 49

Consider the following C code segment.int a, b, c = 0; void prtFun(void); main( ) { static int a = 1; /* Line 1 */ prtFun( ); a += 1; prtFun( ); printf(" %d %d ", a, b); } void prtFun(void) { static int a = 2; /* Line 2 */ int b = 1; a += ++b; printf(" %d %d ", a, b); }What output will be generated by the given code segment?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) 4 2 6 2 2 0

Programming in C

Let's trace the execution of the program step by step:

  1. Global variables:a = 0, b = 0, c = 0 (uninitialized global a and b default to 0).

  2. main() begins:

    • static int a = 1; initializes a static variable a local to main with the value 11.

    • First call to prtFun():

      • static int a = 2; initializes static local a in prtFun to 22.

      • int b = 1; initializes auto local b in prtFun to 11.

      • a += ++b; increments b from 11 to 22, then a becomes 2+2=42 + 2 = 4.

      • printf prints: 4 2.

    • Back in main():

      • a += 1; increments main's static variable a from 11 to 22.

    • Second call to prtFun():

      • static int a retains its value from the previous call (44).

      • int b = 1; re-initializes auto local b to 11.

      • a += ++b; increments b from 11 to 22, then a becomes 4+2=64 + 2 = 6.

      • printf prints: 6 2.

    • Back in main():

      • `printf("

%d %d ", a, b);`: - a refers to main's static local variable, which has the value 22. - Since there is no local variable b in main(), it refers to the global variable b, which is 00. - It prints: 2 0.

Thus, the complete printed output is: ``` 4 2 6 2 2 0 ```

Therefore, option (C) is the correct choice.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q48 of the GATE CSE 2012 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 19 of them are 2-mark MCQs like this one, and GATE CSE 2012 has 3.

How GATE tests Programming in C

More from GATE CSE 2012

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2012 · Q48 · MCQ

Common Data for Questions 48 and 49: Consider the following C code segment. int a, b, c = 0; void prtFun(void); main( ) { static int a = 1; /* Line 1 */ prtFun( ); a += 1; prtFun( ); printf(" %d %d ", a, b); } void prtFun(void) { static int a = 2; /* Line 2 */ int b = 1; a += ++b; printf(" %d %d ", a, b); } What output will be generated by the given code segment?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.