Question 9
Back to Exercise
What is the output of the following C program?
#include <stdio.h>
int depth(int n) {
static int calls = 0;
calls++;
if (n > 0)
depth(n - 1);
return calls;
}
int main(void) {
printf("%d\n", depth(3));
return 0;
}
- A 4 Correct
- B 1
- C 3
- D 0
Correct Answer: Option A
All recursive calls share the single static calls. depth(3), depth(2), depth(1) and depth(0) each add 1, so the outer call returns 4. A non-static local would be 1 in every call.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!