ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
int g = 10;
int f(int n) {
    if (n == 0) return g;
    g += n;
    return f(n - 1);
}
int main(void) { printf("%d %d", f(3), g); }
  1. A. 10 16
  2. B. 16 10
  3. C. 6 16
  4. D. 16 16

Correct answer: D. 16 16

g is global, mutated across every call: 10+3=13, +2=15, +1=16, then n==0 returns g=16. Both the function's return value and the global afterward are 16.

More C Programming questions

Exams that ask this topic