ProgrammingVery hardGATE CS

What is printed?

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

Correct answer: C. 26

The static counter c survives all calls. Calls(n) = 1 + Calls(n-1) + Calls(n-2) with Calls(0) = Calls(1) = 1, so Calls(6) = 25. The later f(0) is call number 26 and returns c = 26.

More C Programming questions

Exams that ask this topic