The integer value printed by the 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;
}
View worked solution
Answer: 7
To find the output of the program, let's trace the execution step-by-step. Note that the variable x inside funcp() is declared as static, meaning it retains its value across multiple function calls and is initialized only once.
Step-by-Step Execution:
-
First call to
funcp():- The static variable
funcp::xis initialized to (this initialization happens only once). x++incrementsfuncp::xto .- The function returns .
- In
main(), the local variablexis assigned this returned value:
- The static variable
-
Second call to
funcp():- The static variable
funcp::xretains its previous value of . x++incrementsfuncp::xto .- The function returns .
- The static variable
-
Evaluating
y = funcp() + xinmain():- The call
funcp()returns . - The local variable
main::xis . - Thus,
- The call
-
Printing the result:
- The program prints the value of
x + y:
- The program prints the value of
Therefore, the integer value printed by the program is 7.
Free practice · Your answers on this page are not saved.
