The integer value printed by the ANSI-C program given below is … #include int funcp(){ static int x = 1; x++; return x; } int main(){ int x,y; x = funcp(); y = funcp()+x; printf("%d ", (x+y)); return 0; }
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2023 · Q35
Time
#include<stdio.h>
int funcp(){
static int x = 1;
x++;
return x;
}
int main(){
int x,y;
x = funcp();
y = funcp()+x;
printf("%d
", (x+y));
return 0;
}Your answerNumerical answer
Solution
Official answer: 7
Programming in CLet us trace the execution of the program step by step:
Inside funcp(), the variable x is declared as static int x = 1;. A static local variable retains its value between successive function calls and is initialized only once.
In main(), local variables x and y are declared.
The statement x = funcp(); executes:
Inside funcp(), x is incremented from to (x++).
funcp() returns .
The local variable x in main is assigned the value .
The statement y = funcp() + x; executes:
The second call to funcp() increments the static variable x from to .
funcp() returns .
The local variable x in main is still .
Therefore, .
The statement `printf("%d
", (x+y));` prints:
Thus, the printed integer value is 7.
Try the question first, then check your work.
Examiner trap
Recall that a static variable preserves its value across multiple function calls, while local variables inside main are separate in scope.
This Programming in C previous year question is Q35 of the GATE CSE 2023 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.
Free diagnostic
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.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 16 of them are 1-mark NAT questions like this one, and it is the only one in GATE CSE 2023.
How GATE tests Programming in CIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.