C Storage Classes
Every variable in C has three properties besides its type:
- Scope — where in the code its name is visible.
- Lifetime (storage duration) — how long it exists in memory.
- Linkage — whether other source files can refer to it.
Storage class specifiers — auto, static, extern and register — let you control these properties.
Overview
| Specifier | Where | Lifetime | Scope | Default value |
|---|---|---|---|---|
auto (default for locals) |
inside a block | until the block ends | the block | garbage |
register |
inside a block | until the block ends | the block | garbage |
static (local) |
inside a block | the whole program | the block | 0 |
static (global) |
outside functions | the whole program | this file only | 0 |
extern |
anywhere | the whole program | where declared | (defined elsewhere) |
auto
Every local variable is auto unless you say otherwise. It is created each time execution enters its block and destroyed when it leaves. Writing the keyword is allowed but never necessary, so you will hardly ever see it:
void f(void) {
auto int x = 1; // exactly the same as: int x = 1;
}Note: In C++ (and in C23),
automeans something completely different: "deduce the type". In C17 it is just the default storage class.
static local variables: remembering between calls
A local variable declared static is created once, when the program starts, and keeps its value between function calls. Its name is still only visible inside the function:
#include <stdio.h>
void count_calls(void) {
int normal = 0; // recreated on every call
static int total = 0; // initialised once, keeps its value
normal++;
total++;
printf("normal = %d, total = %d\n", normal, total);
}
int main(void) {
count_calls();
count_calls();
count_calls();
return 0;
}normal = 1, total = 1
normal = 1, total = 2
normal = 1, total = 3
This is useful for counters, caches and generators that need memory without exposing a global variable:
#include <stdio.h>
int next_id(void) {
static int id = 1000; // private to next_id, lives forever
return ++id;
}
long long fib(int n) {
static long long memo[91]; // all zero at program start
if (n <= 1) return n;
if (memo[n] != 0) return memo[n];
memo[n] = fib(n - 1) + fib(n - 2);
return memo[n];
}
int main(void) {
int a = next_id();
int b = next_id();
int c = next_id();
printf("ids: %d %d %d\n", a, b, c);
printf("fib(90) = %lld\n", fib(90));
return 0;
}ids: 1001 1002 1003
fib(90) = 2880067194370816120
The three IDs are fetched in separate statements on purpose: the order in which a function's arguments are evaluated is unspecified in C, so printf("%d %d", next_id(), next_id()) could print them in either order. The memoised fib stores results in a static array, turning an exponential algorithm into a linear one.
static global variables and functions: private to a file
At file level, static has a different meaning: it limits linkage. A static global variable or function can only be used inside the .c file where it is defined. That is C's way of making things "private":
// counter.c
static int count = 0; // invisible to other files
static void log_change(void) { // helper, also private
/* ... */
}
void counter_increment(void) { // public: other files may call this
count++;
log_change();
}Two different files can each have their own static int count without conflict. Making helpers static by default is good practice: it keeps the public interface of each file small.
extern: sharing a variable across files
A global variable without static can be used from other files. To do so, the other file declares it with extern — "this variable exists, but is defined somewhere else":
// config.c — the one definition
int verbose_level = 2;
// config.h — the declaration everyone includes
extern int verbose_level;
// main.c
#include "config.h"
int main(void) {
if (verbose_level > 1) { /* ... */ }
}Within a single file extern can also refer to a global defined further down:
#include <stdio.h>
int main(void) {
extern int max_users; // declared here, defined below
printf("max_users = %d\n", max_users);
return 0;
}
int max_users = 250; // the definitionmax_users = 250
Functions are extern by default, which is why a prototype in a header is enough to call a function defined in another file.
register
register asks the compiler to keep a variable in a CPU register for speed. Modern compilers ignore the hint because they allocate registers far better than humans. The only lasting effect is that you cannot take the variable's address:
register int i = 0;
int *p = &i; // error: address of register variable requestedYou can safely forget about register in new code.
Static vs. automatic initialisation
Variables with static lifetime (globals and static locals) are initialised once, before main starts, and default to zero. Their initialisers must therefore be constant expressions:
#include <stdio.h>
int global_counter; // 0
static double ratio; // 0.0
int main(void) {
static int calls; // 0
static const char *name; // NULL
printf("%d %.1f %d %s\n", global_counter, ratio, calls, name == NULL ? "NULL" : name);
return 0;
}0 0.0 0 NULL
Tip: Prefer the smallest lifetime and visibility that works: local
autovariables first,staticlocals when a function must remember state, file-levelstaticfor module-private data, andexternglobals only when you really need program-wide state.
Test yourself
What is special about a static local variable?
- It is visible in all functions
- It keeps its value between function calls
- It is stored in a register
- It cannot be changed
What does static mean on a global function or variable?
- It is constant
- It is only visible in the current source file
- It is shared with all files
- It is allocated on the heap
What does extern int limit; do?
- Defines limit and sets it to 0
- Declares limit, which is defined elsewhere
- Makes limit private
- Creates a constant