GateAI
Programming in C

GATE CSE 2012 · Q49

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 if: Line 1 is replaced by auto int a = 1; Line 2 is replaced by register int a = 2;

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: D) 4 2 4 2 2 0

Programming in C

Replacing Line 1 and Line 2 changes the storage classes:

  • Line 1: auto int a = 1; (an automatic variable local to main).

  • Line 2: register int a = 2; (an automatic/register variable local to prtFun, re-initialized on every invocation).

Let's trace the execution:

  1. main() begins:

    • auto int a = 1; initializes main's local a to 11.

    • First call to prtFun():

      • register int a = 2; initializes a to 22.

      • int b = 1; initializes b to 11.

      • a += ++b; increments b to 22, and a becomes 2+2=42 + 2 = 4.

      • printf prints: 4 2.

    • Back in main():

      • a += 1; increments main's local a to 1+1=21 + 1 = 2.

    • Second call to prtFun():

      • register int a = 2; re-initializes a to 22 (since register variables have automatic storage duration and do not retain values across calls).

      • int b = 1; initializes b to 11.

      • a += ++b; increments b to 22, and a becomes 2+2=42 + 2 = 4.

      • printf prints: 4 2.

    • Back in main():

      • `printf("

%d %d ", a, b); prints main's local a ($2$) and the global b` (00). - It prints: 2 0.

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

Therefore, option (D) 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 Q49 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 · Q49 · 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 if: Line 1 is replaced by auto int a = 1; Line 2 is replaced by register int a = 2;

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.