ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int f(int n) {
    static int calls = 0;
    calls++;
    if (n <= 0) return calls;
    int r = f(n - 2);
    return r * 10 + calls;
}
int main(void) {
    printf("%d", f(4));
}
  1. A. 212
  2. B. 333
  3. C. 101
  4. D. 4441

Correct answer: B. 333

The static counter is shared by all calls, so the recursive call increments it before the outer call reads it. Storing the recursive result before reading calls keeps the order defined.

More C Programming questions

Exams that ask this topic