ProgrammingVery hardGATE CS

What is the exact output?

What is the exact output?
#include <stdio.h>
void p(int n) {
    if (n <= 0) return;
    printf("%d", n);
    p(n - 1);
    printf("%d", n);
    p(n - 2);
}
int main(void) {
    p(4);
    return 0;
}
  1. A. 43211231142112
  2. B. 43211234112112
  3. C. 4321123112
  4. D. 43211231142121

Correct answer: A. 43211231142112

Tracing p(4): prints 4, p(3) prints 3, p(2) prints 2, p(1) prints 1, returns through p(0), prints 1, p(-1) returns; back in p(2) prints 2, then p(0); in p(3) prints 3 and calls p(1) which prints 11; in p(4) prints 4 and calls p(2) which prints 2112, giving 43211231142112.

More C Programming questions

Exams that ask this topic