Skip to content
Official PYQ · 2019

GATE CS 2019 Q27 — Programming in C

Explain with AI

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?

Choose one option
View worked solution

Answer: B

The function r() uses a static variable num initialized to 77. The post-decrement operator num-- returns the current value of num and then decrements it by 11.

Let's trace the execution of the for loop step-by-step:

  1. Initialization: r() is called.
    • Returns 77 (ignored).
    • num becomes 66.
  2. First Iteration:
    • Condition: r() is evaluated. Returns 66 (non-zero, so true). num becomes 55.
    • Body: printf("%d", r()) is executed. r() returns 55 and num becomes 44. This prints 5.
    • Increment: r() is called. Returns 44. num becomes 33.
  3. Second Iteration:
    • Condition: r() is evaluated. Returns 33 (non-zero, so true). num becomes 22.
    • Body: printf("%d", r()) is executed. r() returns 22 and num becomes 11. This prints 2.
    • Increment: r() is called. Returns 11. num becomes 00.
  4. Third Iteration:
    • Condition: r() is evaluated. Returns 00 (zero, so false). num becomes −1-1.
    • The loop terminates.

The printed output is 52, which corresponds to option B.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions