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

  1. A.

    6

  2. B.

    8

  3. C.

    10

  4. 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.

Explore the full course: Up Lt Grade Assistant Teacher 2025