GATE 2019 CS – Question 37
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?
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.