What will be the output of the following program? int f(int x) { static int y;…
2018
What will be the output of the following program? int f(int x) { static int y; y += x; return (y); } main() { int a, i; for (i = 0; i < 6; i++) a = f(i); printf("%d", a); }
- A.
6
- B.
8
- C.
10
- D.
15
Attempted by 278 students.
Show answer & explanation
Correct answer: D
Explanation: The variable y is declared static, so it is initialized once to 0 and keeps its value across function calls. The loop calls f(i) for i = 0 to 5.