- Why each function's
autovariables are completely separate objects - How the call stack keeps an outer function's locals alive while the inner function runs
- Why the inner function cannot "see" the outer function's locals without explicit parameters
- How name shadowing works when two nested calls declare the same local name
- Why C does not support nested function definitions like Python or JavaScript (GCC's extension is non-standard)
What "Nested Function Calls" Means Here
In Python or JavaScript you can define a function inside another function, and the inner function captures the outer function's variables. C does not work that way. Standard C does not allow function definitions inside other functions — though GCC offers a non-standard extension for it. When C programmers say "nested function calls," they almost always mean a call chain: main() calls outer(), which calls middle(), which calls inner(). Each function is defined at file scope, but the calls stack up at runtime.
The question then is: what happens to auto variables during this chain? Specifically:
- Does
inner()see the auto variables ofmiddle()? No. - Are the auto variables of
middle()destroyed whileinner()runs? No. - If
inner()declares a variable with the same name as one inmiddle(), does it overwrite it? No — it shadows it.
The reason is that auto locals live on the call stack. Each function call pushes its own frame; the frame for middle() remains underneath inner()'s frame for the duration of the inner call. The two frames are independent — the inner one simply sits on top. This tutorial walks through exactly this behaviour with a runnable program.
💡 In one line: Each call gets its own stack frame. Auto variables in one frame are invisible to other frames — they die only when their own function returns.
Scope, Lifetime, and Visibility Rules
| Concept | For an auto variable in outer() while inner() runs |
|---|---|
| Scope | Still only outer(). inner() cannot refer to it by name. |
| Lifetime | Still alive — outer() hasn't returned yet, so its frame still exists. |
| Visibility | Invisible to inner() unless its address was passed as an argument. |
| Storage | Still on the stack, in outer()'s frame, just under inner()'s frame. |
| Shadowing | If inner() declares the same name, it hides the outer one entirely for the inner function body. |
Complete C Program
Program: demonstrate scope and visibility of auto variables in a call chain
#include <stdio.h>
/* Forward declarations */
void outer(int depth);
void middle(int depth);
void inner(int depth);
/* --- The innermost function --- */
void inner(int depth) {
auto int level = depth;
/* These are inner's OWN auto variables.
It cannot see outer's or middle's locals. */
auto int value = 999;
auto int shared_name = 30; /* shadows any outer 'shared_name' */
printf(" [inner ] depth=%d value=%d shared_name=%d\n",
level, value, shared_name);
}
/* --- The middle function --- */
void middle(int depth) {
auto int level = depth;
auto int value = 200; /* own value — independent of inner's */
auto int shared_name = 20; /* shadows outer's shared_name */
printf(" [middle] depth=%d value=%d shared_name=%d\n",
level, value, shared_name);
/* Nested call — middle's frame stays on the stack */
inner(depth + 1);
/* After inner() returns, middle's locals are unchanged */
printf(" [middle] after inner: value=%d shared_name=%d\n",
value, shared_name);
}
/* --- The outer function --- */
void outer(int depth) {
auto int level = depth;
auto int value = 100;
auto int shared_name = 10;
printf("[outer ] depth=%d value=%d shared_name=%d\n",
level, value, shared_name);
middle(depth + 1);
printf("[outer ] after middle: value=%d shared_name=%d\n",
value, shared_name);
}
int main(void) {
auto int main_local = 7;
printf("=== Auto variables in a nested call chain ===\n");
outer(0);
printf("\n=== Passing addresses lets callees see caller's autos ===\n");
printf("main_local = %d\n", main_local);
return 0;
}
Program Output
=== Auto variables in a nested call chain ===
[outer ] depth=0 value=100 shared_name=10
[middle] depth=1 value=200 shared_name=20
[inner ] depth=2 value=999 shared_name=30
[middle] after inner: value=200 shared_name=20
[outer ] after middle: value=100 shared_name=10
=== Passing addresses lets callees see caller's autos ===
main_local = 7
Three observations stand out. First, each level printed its own value (100, 200, 999) — proving they are three separate objects. Second, when inner() declared shared_name = 30, it did not disturb the shared_name = 20 in middle() — because the inner one shadowed the outer one within its own scope. Third, when control returned to middle(), its value and shared_name were exactly where they started. The nested call had no effect.
Step-by-Step Explanation
Let's trace what happens on the call stack, frame by frame.
1. main() starts — main_local goes on the stack:
main() pushes its frame, containing main_local = 7. Its lifetime is the entire program run; its scope is the body of main().
2. outer() is called — its frame goes on top:
outer() creates its own level, value, and shared_name. These are brand-new objects, unrelated to anything in main(). Even if main() had a variable called value, they would be different memory locations.
3. middle() is called — another frame is pushed:
void middle(int depth) {
auto int level = depth;
auto int value = 200;
auto int shared_name = 20;
...
}
From this point until middle() returns, there are two value objects in memory: outer()'s (=100) and middle()'s (=200). The middle function's code can only see its own. outer()'s copy sits silently under it on the stack.
4. inner() is called — a third frame is pushed:
Now three value objects exist: 100, 200, 999. The shared_name declared inside inner() (30) hides the ones in middle() (20) and outer() (10) — but only from within inner()'s body. This is classic shadowing: an inner declaration wins within its own scope but leaves the outer objects untouched.
5. inner() returns — its frame pops off:
The 999 and 30 values vanish with the frame. Control returns to middle(), whose locals are still exactly where they were. Output line [middle] after inner: value=200 shared_name=20 proves it.
6. middle() returns — its frame pops off:
Now outer() is back in control. Same story: its 100 and 10 are untouched. Output confirms.
7. What if the caller wants to share an auto variable?
The callee has no way to reach the caller's locals by name. The C-idiomatic answer is: pass the address. Change the callee to accept int *p and write *p = ..., and now both frames operate on the same object. That's exactly how C functions communicate changes back to their caller — and it's the reason the language has pointers. Without an explicit parameter, the callee's private world stays private.
8. Why doesn't C support nested function definitions?
Standard C requires function definitions at file scope. GCC offers a non-standard extension (nested functions with access to enclosing locals via trampolines), but this is not portable, is disabled under -std=c11 (unless -fgnu89-inline or similar is set), and is generally discouraged for production code. If you need closures in C, pass a context struct — that's what everyone does.
🎯 Remember this: The scope of an auto variable is the block that declares it, and nothing more. The lifetime is until that block exits. During a nested call, an outer auto variable is alive but invisible from the callee. Pass a pointer if you need the callee to access it.
Frequently Asked Questions
1. Is "nested functions" the same as "nested function calls"?
No, and this trips up many beginners. Nested functions means defining a function inside another function's body — not standard C, though GCC supports it as an extension. Nested function calls means one function calling another, which is completely standard. This tutorial covers the second meaning — the call chain.
2. Does the inner function really not see the outer's locals?
Correct. The inner function has no name for the outer's variables. They may sit just a few bytes away on the stack, but the C compiler refuses to allow inner() to reference them by name. This is intentional: it prevents accidental coupling and makes each function easier to reason about. If you want to share, pass a pointer.
3. What if I pass a pointer to a local and the callee stores it?
That's a dangling pointer hazard. If you pass &local to inner() and inner() stores that pointer, then when outer() returns, the pointer becomes invalid. Any later dereference is undefined behaviour. Only store addresses of objects with lifetimes at least as long as the pointer's.
4. What about recursion — doesn't the same variable get "reused"?
No. Each recursive call has its own stack frame, so its own copy of every auto variable. A recursive function with a local int depth has a fresh depth per level. That's exactly why recursion works: each call's state is isolated. Passing state by static variable would break this — which is why recursion and statics rarely mix well.
5. Can I inspect the stack to confirm this?
Yes. Print the addresses (printf("%p", (void*)&value)) of each function's local and you'll see them at distinct addresses, all in the stack region, generally decreasing as you go deeper in the call chain (on most architectures). Running the program under gdb and using the bt (backtrace) command shows the frames stacked up.
6. What's the difference between "not visible" and "not alive"?
Very important distinction. A variable is alive if its memory still exists and holds a valid value. It's visible if the compiler allows your code to name it. During a nested call, the outer's auto variables are still alive (their frame is on the stack) but not visible (the inner function's scope doesn't include them). Once the outer function returns, the variables become both dead and invisible.
💡 Tip: If you find yourself wanting an inner function to access an outer function's locals, stop and consider the design. Either pass the data explicitly (the C way) or bundle it into a struct. Both make dependencies obvious; neither creates hidden couplings that future maintainers have to reverse-engineer.