Q
What is the output of this C++ code?
int f(){ static int c = 0; return ++c; }
int main(){ f(); f(); cout
-
A
1
-
C
3
-
D
Compilation error
Correct Answer:
C. 3
Explanation:
A static local variable is initialized once and keeps its value between calls, so the calls return 1, 2, and then 3.