Correct option is B
Let’s analyze the code:
1.
Static Variable Behavior:
· The variable i is declared as static inside the function func().
· Static variables are initialized only once, retain their value between function calls, and persist for the lifetime of the program.
· i is initialized to 7 during the first call to func() and is not reinitialized in subsequent calls.
2.
First Call to func():
· i = 7 (initialization happens here).
· The value of i is printed: 7.
· i is incremented: i = 8.
3.
Second Call to func():
· i retains its value from the previous call (i = 8).
· The value of i is printed: 8.
· i is incremented: i = 9.
4.
Third Call to func():
· i retains its value from the previous call (i = 9).
· The value of i is printed: 9.
· i is incremented: i = 10 (though this is not printed).
Final Output:
The program prints:
7 8 9
Key Points:
1.
Static Variables:
· Retain their value between function calls.
· Are initialized only once during their first encounter in the program.
· Persist for the program's lifetime.
2.
Function Calls: Each call to func() uses the updated value of the static variable i from the previous function call.
Knowledge Booster:
·
Difference Between Static and Automatic Variables:
·
Automatic Variables: Declared without static, are reinitialized every time the function is called.
·
Static Variables: Preserve their value across multiple function calls.
·
Use Cases of Static Variables:
· Counting function calls.
· Retaining state information in recursive or iterative functions.