Multiple choice

void myFunc (int x) { if (x > 0) myFunc(--x); printf(%d, , x); } int main() { myFunc(5); return 0; } What will the output of the above code after execution?

  1. 1, 2, 3, 4, 5, 5

  2. 4, 3, 2, 1, 0, 0

  3. 5, 4, 3, 2, 1, 0

  4. 0, 0, 1, 2, 3, 4

  5. 0, 1, 2, 3, 4, 5

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

myFunc recursively calls itself with decremented x until x=0, then unwinds printing each x value. For x=5, the call stack builds: myFunc(5)->myFunc(4)->myFunc(3)->myFunc(2)->myFunc(1)->myFunc(0). At x=0, recursion stops. Then prints happen during unwinding: 0,0,1,2,3,4 (note: myFunc(0) prints 0 twice - once after base case, and main's myFunc(5) eventually prints 0 after all recursive calls complete).