ProgrammingVery hardGATE CS · Accenture

What is printed? int f(int n) { static int c = 0; c += n; if (n <= 0) return c; int r = f(n - 1); return r + c; } printf("%d", f(3));

  1. A. 24
  2. B. 18
  3. C. 12
  4. D. 30

Correct answer: A. 24

The static c grows to 3, 5, 6 and 6 on the way down, so f(0) returns 6. Each return then adds the final shared c = 6: f(1) = 12, f(2) = 18, f(3) = 24. Treating c as a per-call local gives the trap values.

More C Programming questions

Exams that ask this topic