Question:

The integer value printed by the ANSI-C program given below is ________.

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;
}

Show Hint

Remember that static} variables inside a function preserve their value between successive calls. This is often tested in C programming exam questions.
Updated On: Aug 26, 2025
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Understanding static int x in funcp()
The variable x inside funcp() is declared as static, so it retains its value across multiple function calls. Initially, x = 1.
Step 2: First function call (in x = funcp();)
- On entry, x = 1.
- Increment: x++ makes it 2.
- Function returns 2.
So now in main(), x = 2.
Step 3: Second function call (in y = funcp() + x;)
- On entry, the static variable x inside funcp() is now 2 (retained from last call).
- Increment: x++ makes it 3.
- Function returns 3.
Thus, y = 3 + x(main) = \(3 + 2 = 5\).
Step 4: Final output (printf)
The program prints \((x + y) = (2 + 5) = 7\). \[ \boxed{7} \]
Was this answer helpful?
0
0

Top Questions on Engineering Mathematics

View More Questions

Questions Asked in GATE CS exam

View More Questions