- What the
autostorage class means — and why it's the default for locals - How
autovariables live on the stack, created at block entry and destroyed at block exit - How an inner
autoshadows an outer one with the same name - Why each function call gets a fresh copy — no persistence between calls
- Why trying to access an
autovariable outside its block is a compile error
What is the auto Storage Class?
The auto storage class is the default for any variable declared inside a function or block. Writing auto explicitly is legal but redundant — its main use in teaching is to make the concept visible. An auto variable:
- Lives on the stack
- Exists only for the duration of its block
- Starts with garbage (uninitialized) unless you assign a value
- Is re-created every time its enclosing function is called
💡 Quick Definition: auto means "automatic storage duration" — the compiler allocates the variable automatically when its block begins, and releases it automatically when the block ends. You never call malloc or free for an auto variable.
Key Rules
| Property | Value |
|---|---|
| Storage | Stack (or CPU register if optimized) |
| Default value | Garbage (uninitialized) |
| Scope | Inside the enclosing block / function |
| Lifetime | From block entry to block exit |
| Linkage | None |
| Can be declared global? | No — auto at file scope is a compile error |
Complete C Program
Program: auto variables, shadowing, and scope
#include <stdio.h>
/* Function 1: demonstrates that each call gets a FRESH auto variable */
void showAuto(void) {
auto int x = 10; /* created fresh on every call */
x = x + 5;
printf("Inside showAuto(): x = %d\n", x);
}
/* Function 2: demonstrates shadowing of an outer variable by an inner one */
void showShadowing(void) {
int x = 100; /* outer auto variable */
printf("Outer x before block : %d\n", x);
{
int x = 500; /* inner auto variable shadows the outer one */
printf("Inner x inside block : %d\n", x);
}
printf("Outer x after block : %d\n", x); /* still 100 */
}
/* Function 3: demonstrates that auto variables do not persist */
void showNoPersistence(void) {
int count = 0; /* re-initialized each call */
count = count + 1;
printf("count in this call : %d\n", count);
}
int main(void) {
printf("--- Each call creates a fresh auto variable ---\n");
showAuto();
showAuto();
printf("\n--- Inner auto shadows outer auto ---\n");
showShadowing();
printf("\n--- Auto variables do not persist between calls ---\n");
showNoPersistence();
showNoPersistence();
return 0;
}
Program Output
--- Each call creates a fresh auto variable ---
Inside showAuto(): x = 15
Inside showAuto(): x = 15
--- Inner auto shadows outer auto ---
Outer x before block : 100
Inner x inside block : 500
Outer x after block : 100
--- Auto variables do not persist between calls ---
count in this call : 1
count in this call : 1
Step-by-Step Explanation
Let's break down each function and what it demonstrates.
1. Include header and function declarations:
#include <stdio.h>
void showAuto(void);
void showShadowing(void);
void showNoPersistence(void);
stdio.h provides printf. The three function prototypes are just declarations — C requires them when main appears before the function definitions.
2. showAuto — each call gets a fresh copy:
void showAuto(void) {
auto int x = 10;
x = x + 5;
printf("Inside showAuto(): x = %d\n", x);
}
Each call re-runs the initialization x = 10, then adds 5 and prints 15. Because x is auto, its value from the previous call is not remembered — you get 15 every time, not 15 then 20.
3. showShadowing — inner shadows outer:
void showShadowing(void) {
int x = 100;
printf("Outer x before block : %d\n", x);
{
int x = 500;
printf("Inner x inside block : %d\n", x);
}
printf("Outer x after block : %d\n", x);
}
The inner x = 500 is a different variable — same name, different block, different stack slot. Inside the nested block, the inner one wins. When the block ends, the outer x reappears with its original value (100). This is called shadowing.
4. showNoPersistence — no memory of prior calls:
void showNoPersistence(void) {
int count = 0;
count = count + 1;
printf("count in this call : %d\n", count);
}
If count were static, it would print 1 then 2. Because it's auto, it resets to 0 every call — hence 1 and 1.
5. Attempting to use x outside its block:
{
int x = 500;
/* ... */
}
/* x is GONE here — this line would NOT compile: */
/* printf("%d", x); --> error: 'x' undeclared */
This is the compiler enforcing scope. If you uncomment that line, GCC reports error: 'x' undeclared — proving that auto variables are strictly block-scoped.
🎯 Key Takeaway: auto is the storage class you never have to write — because it's the default. But understanding why it's the default (stack allocation, automatic lifetime, no persistence) is what lets you reason about scope bugs and decide when to switch to static for persistence or register for speed.
Frequently Asked Questions
1. Do I ever need to write the keyword auto?
Almost never. In C, auto is the default for locals — omitting it changes nothing. The keyword is required in a few grammatically obscure cases (e.g., auto int x, y; where the second declarator inherits), but in practice you'll only see it in tutorials. In C++, auto has a completely different meaning — type inference.
2. What is the default value of an auto variable?
Garbage. Unlike static and global variables (which are zero-initialized), auto variables contain whatever was on the stack at that moment. Never read an auto variable before assigning to it.
3. Can I declare an auto variable outside a function?
No. auto int x; at file scope is a compile error in standard C. Global variables have extern linkage — if you want internal file scope, use static.
4. Does the compiler ever store an auto variable in a CPU register?
Yes, routinely. Once the variable's address is never taken and the optimizer sees the benefit, it can be kept entirely in a register — even without the register keyword. Writing register is a hint; the compiler decides.
5. What is the stack depth limit for auto variables?
On typical Linux systems, the stack is limited to around 8 MB. Recursion with large auto arrays can exhaust it — often the cause of "stack overflow" crashes. In such cases, allocate large blocks on the heap with malloc instead.
💡 Tip: Compile with gcc -Wall -Wextra to get warnings when you read an uninitialized auto variable.