The GATE Grind

GATE 2019 CS – Question 37

Programming and Data Structures · Programming in C · 2 marks · Multiple choice

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?

  1. 41
  2. 52
  3. 63
  4. 630

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (B) 52

Explanation

The initial call returns 7 (num becomes 6). The condition calls $r()$ giving 6 and body prints $r()=5$. The increment calls $r()=4$, the condition gets 3, and the body prints $r()=2$. The increment gets 1 and the condition then gets 0, so the loop stops. The output is 52.