Storage Classes in C
A storage class in C controls three things about a variable: its scope (where it's visible), its lifetime (how long it lives in memory), and its linkage (whether it can be shared across files). The four storage class keywords are auto, static, extern, and register. Getting these right explains why a variable "remembers" its value — or doesn't.
auto: the default for locals
Every local variable is auto by default, so you rarely write the keyword. An auto variable lives only while its block runs, and is re-created each time:
void demo(void) {
auto int count = 0; /* same as: int count = 0; */
count++; /* always 1 here -- reset on every call */
}
Its scope is the block it's declared in; its lifetime ends when the block exits.
static: remembers between calls
A static local variable keeps its value between function calls. It is initialized once and persists for the whole program:
#include <stdio.h>
void counter(void) {
static int calls = 0; /* initialized once, retained after */
calls++;
printf("Called %d time(s)\n", calls);
}
int main(void) {
counter(); /* Called 1 time(s) */
counter(); /* Called 2 time(s) */
counter(); /* Called 3 time(s) */
return 0;
}
static has another use at file level: a static global variable or function is visible only within its own source file — a way to keep helpers private.
extern: sharing across files
extern declares that a variable is defined elsewhere (usually in another file), letting you share one global across multiple source files:
/* file1.c */
int total = 0; /* the actual definition */
/* file2.c */
extern int total; /* "this exists somewhere else" */
extern says "trust me, the linker will find it." Without it, each file would think it owns a separate variable.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C Programmingregister: a hint (largely historical)
register suggests the compiler keep a variable in a CPU register for speed. Modern compilers optimize so well that this hint is mostly ignored, and you cannot take the address of a register variable with &:
register int i; /* a hint only -- the compiler may ignore it */
You'll rarely need it today; it's worth knowing for older code and exams.
Scope and lifetime summary
| Storage class | Scope | Lifetime |
|---|---|---|
auto (default local) | block | block execution |
static (local) | block | whole program |
static (global/function) | file only | whole program |
extern | across files | whole program |
register | block | block execution |
Common mistakes
- Expecting an
autolocal to remember its value between calls — it resets each time. Usestaticfor memory. - Defining a global in a header and including it in many files — that creates duplicate definitions. Define once in a
.cfile, declare withexternin the header. - Taking the address of a
registervariable — not allowed. - Confusing the two meanings of
static: inside a function it means "persistent lifetime"; at file scope it means "private to this file." - Relying on uninitialized
autovariables — they hold garbage;staticvariables default to zero.
FAQ
Why does my counter reset to 1 every call? It's an auto local. Make it static to keep its value.
Is register still useful? Almost never — trust the optimizer. Know it for legacy code.
Continue at the hub C Programming, then Functions in C and Variables & Data Types.
Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in C
A beginner guide to arrays in C — declaring, initializing and indexing them, looping over elements, multidimensional arrays, and avoiding out-of-bounds errors.
Common C Errors & How to Fix Them
A troubleshooting guide to the most common C errors beginners face — from = vs ==, format mismatches and missing semicolons to segmentation faults — and how to fix each.
Dynamic Memory Allocation in C
A beginner guide to dynamic memory allocation in C — malloc, calloc, realloc and free, allocating at runtime, and preventing memory leaks.
