ProgrammingHardWipro · Cognizant pattern

What is printed?

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

Correct answer: D. 10

Each call adds the current n to *x via the shared pointer before recursing: x accumulates 4+3+2+1 = 10, and the base case (n==0) returns the final *p = 10.

More C Programming questions

Exams that ask this topic