GATE 2023 CS – Question 35
The integer value printed by the ANSI-C program given below is ______.
#include<stdio.h>
int funcp(){
static int x = 1;
x++;
return x;
}
int main(){
int x,y;
x = funcp();
y = funcp()+x;
printf("%d\n", (x+y));
return 0;
}Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: 7
Explanation
`static int x = 1;` inside `funcp` is created once and keeps its value between calls (the `x` in `main` is a different variable).
- First call: `x++` makes the static $x=2$ and returns 2. So `main`'s $x=2$.
- Second call: static $x$ becomes 3 and returns 3. So $y=3+x=3+2=5$.
The program prints $x+y=2+5=\mathbf7$.