- Why every recursive call gets its own copy of an
autovariable - Why that's good — it keeps calls independent and reentrant
- Why that's sometimes bad — you can't use a plain auto to accumulate across calls
- How to thread state through recursion correctly (extra parameter, not global)
- Why very deep recursion eventually crashes the program (stack overflow)
Why Does Recursion Change Things?
In a non-recursive function, an auto variable lives once per call. In a recursive function, the same function calls itself — so the same line of code runs many times, each with its own fresh copy of the variable on the stack.
That is the whole story of auto variables in recursion: one variable per call, stacked one on top of another. It's an advantage when each call needs its own independent state. It's a limit when you want a single value to be shared — because a plain auto variable simply won't do that.
💡 In one line: Every recursive call gets its own auto variables — great for keeping calls independent, useless for accumulating a value across calls.
Advantages and Limits
| Advantage | Limit |
|---|---|
| Each recursive call is independent — no shared state to corrupt | You cannot accumulate a single value across calls using a plain auto |
| Naturally thread-safe within a call — no interference | Deep recursion uses a lot of stack (one frame per call) |
| Cleaner re-entrant code: the function can be safely re-entered from any depth | Too many auto locals per frame ⇒ stack overflow at lower recursion depth |
| Easy to reason about: the value at depth N has nothing to do with depth N-1 | Passing state requires extra parameters, which changes the function signature |
Complete C Program
Program: Auto variables — correct use vs the classic mistake
#include <stdio.h>
/* ---------------------------------------------
Version 1: WRONG - tries to accumulate
in an auto variable, which resets each call
--------------------------------------------- */
int badFactorial(int n) {
int result = 1; /* auto - reset every call */
if (n == 0) return 1;
result = n * badFactorial(n - 1);
return result;
}
/* ---------------------------------------------
Version 2: CORRECT - state passed as a
parameter, each call independent
--------------------------------------------- */
int goodFactorial(int n) {
if (n == 0) return 1;
return n * goodFactorial(n - 1);
}
/* ---------------------------------------------
Version 3: Show that each call has its own
auto variable via a "depth" print
--------------------------------------------- */
void countdown(int n) {
int localCopy = n; /* auto - one per call */
printf("countdown(%d): localCopy = %d\n", n, localCopy);
if (n > 1) {
countdown(n - 1);
}
printf("countdown(%d): returning, localCopy still = %d\n",
n, localCopy);
}
int main(void) {
printf("--- bad: plain auto accumulator doesn't work ---\n");
printf("badFactorial(5) = %d\n\n", badFactorial(5));
printf("--- good: state passed as a parameter ---\n");
printf("goodFactorial(5) = %d\n\n", goodFactorial(5));
printf("--- per-call auto variable in recursion ---\n");
countdown(3);
return 0;
}
Program Output
--- bad: plain auto accumulator doesn't work ---
badFactorial(5) = 120
--- good: state passed as a parameter ---
goodFactorial(5) = 120
--- per-call auto variable in recursion ---
countdown(3): localCopy = 3
countdown(2): localCopy = 2
countdown(1): localCopy = 1
countdown(1): returning, localCopy still = 1
countdown(2): returning, localCopy still = 2
countdown(3): returning, localCopy still = 3
Notice both factorial versions produce 120 — but the first one happens to work only because result is used within a single call, not accumulated across calls. The real proof that auto variables are per-call comes from countdown: each depth has its own localCopy, and each one survives until its own call returns.
Step-by-Step Explanation
Let's walk through each piece.
1. The "bad" factorial:
int badFactorial(int n) {
int result = 1;
if (n == 0) return 1;
result = n * badFactorial(n - 1);
return result;
}
This looks like it might fail — result is a plain auto, reset on every call. But it works fine here because result is only used to hold one intermediate value within that call. Nothing accumulates across calls. So it's the right answer, but with a fragile pattern — an easy place to accidentally introduce bugs.
2. The "good" factorial — pure recursion:
int goodFactorial(int n) {
if (n == 0) return 1;
return n * goodFactorial(n - 1);
}
No locals needed at all. The stack frames provide the state. Each call knows only its own n, and the multiplication happens on the way back up. This is the cleanest form of recursion.
3. countdown — showing per-call locals:
void countdown(int n) {
int localCopy = n;
printf("countdown(%d): localCopy = %d\n", n, localCopy);
if (n > 1) countdown(n - 1);
printf("countdown(%d): returning, localCopy still = %d\n", n, localCopy);
}
When you call countdown(3), it creates localCopy = 3, prints, then calls countdown(2). That inner call creates its own localCopy = 2 on top of the stack. Then countdown(1) creates another. After the deepest call returns, the outer frames print their own localCopy on the way out. Each value stayed intact because each was a separate variable. That's the advantage of auto in recursion — clean independence.
4. The limit — deep recursion and stack space:
Every recursive call uses a stack frame. Every auto variable in that call lives inside that frame. If the function has 10 auto variables and you recurse 100,000 times, that's 100,000 stack frames. On most systems the stack is 8 MB, so you'll get a stack overflow long before that. That's the limit — auto variables cost stack space per call. A static variable costs just one slot for the whole program.
5. When you need shared state, don't use plain auto:
If you need to accumulate something across recursive calls, either pass it as an extra parameter (the clean way) or use a static variable (which lives for the program). A plain auto inside a recursive function is never shared — that's the wrong tool for that job.
🎯 Remember this: auto in recursion means "one per call". Use that for independence. If you need sharing across calls, use a parameter — not a global, not a static, unless you really can't pass it. And watch the stack depth: every recursive call costs memory.
Frequently Asked Questions
1. Why does badFactorial still work if the variable resets each call?
Because it never needs to accumulate across calls. Each call uses result to hold one intermediate value, computes the next, and returns. The reset is harmless here. The danger is that the code looks like it's accumulating — which misleads beginners. The correct version removes the variable entirely.
2. How do I count how many times a recursive function was called?
Use a static local counter, not an auto. static int calls = 0; calls++; at the top of the function will accumulate across every call, because static lives for the whole program.
3. How deep can recursion go before a stack overflow?
Depends on your compiler and OS, but typically between 10,000 and 100,000 frames. Fewer auto variables per frame means you can go deeper. If your recursion can exceed that depth, rewrite it iteratively (with a loop) or convert it to tail recursion if your compiler supports tail-call optimization.
4. Is recursion always slower than iteration?
Usually yes — each call has overhead (push frame, return address, cleanup). For simple patterns (counting, summing), an iterative loop is faster and uses less memory. For problems with a naturally recursive structure (tree traversal, backtracking), recursion is often clearer and sometimes just as fast.
5. Can I use a static inside recursion to memoize results?
You can, but it's fragile — the static cache is shared between all callers and never resets. If you call fib(10), get an answer, then call fib(5), the cache is still warm — fine. But if the input can change meaningfully (e.g., different modules in a test harness), you need explicit cache management. Prefer passing a cache array as a parameter for clarity.
💡 Tip: If you're testing recursion on a Linux box, run ulimit -s to see your stack limit (usually 8 MB). You can raise it with ulimit -s 65536 for deeper recursion, but doing so in production code is a smell — rewrite the algorithm instead.