GateAI
Programming in C

GATE CSE 2019 · Q27

MCQ2 marksMedium

Time

Consider the following C program:#include <stdio.h> int r(){ static int num=7; return num--; } int main(){ for (r();r();r()) printf("%d",r()); return 0; }Which one of the following values will be displayed on execution of the programs?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) 52

Programming in C

The function r() uses a static variable num initialized to 77. Each call to r() returns the current value of num and then decrements it (num--).

Let us trace the for (init; condition; update) loop step by step:

  1. Initialization (r()):

    • r() returns 77, num becomes 66.

  2. First iteration:

    • Condition check (r()): returns 66 (non-zero/true), num becomes 55.

    • Loop body (printf("%d", r())\):r() returns 55, num becomes 44. Prints 5.

    • Update expression (r()): returns 44, num becomes 33.

  3. Second iteration:

    • Condition check (r()): returns 33 (non-zero/true), num becomes 22.

    • Loop body (printf("%d", r())\):r() returns 22, num becomes 11. Prints 2.

    • Update expression (r()): returns 11, num becomes 00.

  4. Third iteration:

    • Condition check (r()): returns 00 (zero/false), num becomes −1-1.

    • The loop terminates.

Combining the printed outputs in order yields 52.

Thus, option (B) is correct.

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 Q27 of the GATE CSE 2019 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 2019 has 3.

How GATE tests Programming in C

More from GATE CSE 2019

Share this solution

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

GateAI solution cardGATE CSE 2019 · Q27 · MCQ

Consider the following C program: #include int r(){ static int num=7; return num--; } int main(){ for (r();r();r()) printf("%d",r()); return 0; } Which one of the following values will be displayed on execution of the programs?

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.