ProgrammingVery hardIBM · Deloitte pattern

What is printed?

What is printed?
#include <stdio.h>
int f(int n) {
    static int sum = 0;
    sum += n;
    if (n == 0) return sum;
    return f(n - 1);
}
int main(void) {
    printf("%d %d", f(3), f(2));
}
  1. A. 6 3
  2. B. 5 9
  3. C. 6 9
  4. D. 6 8

Correct answer: C. 6 9

sum is static, shared across every call. f(3) adds 3,2,1,0 giving sum=6. The second top-level call f(2) does not restart sum at 0; it continues from 6, adding 2,1,0 to reach 9.

More C Programming questions

Exams that ask this topic