Question:medium

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: Feb 3, 2026
Show Solution

Solution and Explanation

Step 1: Track the lifetime of variables

The variable declared inside funcp() has static storage duration. This means its memory is allocated once and its value persists between calls. The variable is initialized only on the first call with value 1.


Step 2: Evaluate the first assignment

During the first execution of funcp(), the stored value is incremented before being returned.

Stored value after increment = 2

Hence, the variable x in main() receives the value 2.


Step 3: Evaluate the expression in the second assignment

The same stored variable inside funcp() is accessed again. Its current value is incremented once more and returned.

Returned value = 3

The expression being evaluated is:

3 + 2 = 5

So the variable y is assigned the value 5.


Step 4: Compute the printed result

The output statement prints the sum of the two variables in main():

2 + 5 = 7


Final Answer:

7

Was this answer helpful?
0

Top Questions on Engineering Mathematics