Lesson 3 of 5
Storage Classes - auto, register, extern, static & volatile
Every C variable has a **storage class** that determines its lifetime, linkage, and storage location. `auto` (default for locals) - stack, no linkage. `static` - persistent storage, internal linkage for file-scope. `extern` - declares a variable defined elsewhere, external linkage. `register` - hint to use a CPU register (mostly obsolete). `volatile` - tells the compiler NOT to optimise away accesses - critical for hardware registers and signal handlers.
Storage Classes & volatile - With Hardware Register Example
/* ── file_a.c ───────────────────────────────────────────── */
#include <stdio.h>
/* External linkage - accessible from other translation units */
int shared_counter = 0; /* definition in file_a.c */
static int private_counter = 0; /* only visible in file_a.c */
void increment_shared(void) { shared_counter++; }
void increment_private(void) { private_counter++; }
/* ── file_b.c ───────────────────────────────────────────── */
/* extern int shared_counter; - declaration, not definition */
/* extern tells compiler: this is defined somewhere else */
/* The linker resolves it to file_a.c's shared_counter */
/* ── volatile - for hardware registers & signals ──────────
Without volatile, the compiler may optimise:
while (status_reg == 0) {} → read status_reg ONCE,
and spin forever because it doesn't know it can change. */
/* Simulated hardware status register (normally memory-mapped) */
volatile int hw_status_reg = 0;
void wait_for_hardware(void) {
/* The volatile keyword forces the compiler to re-read
hw_status_reg on EVERY loop iteration. */
while (hw_status_reg == 0) {
/* Wait for hardware to set the register to non-zero */
}
printf("Hardware ready
");
}
/* ── volatile for signal handlers ─────────────────────────
A signal handler may modify a variable outside normal flow.
Without volatile, the compiler may cache it in a register. */
#include <signal.h>
volatile sig_atomic_t stop_flag = 0;
void sigint_handler(int sig) {
(void)sig;
stop_flag = 1; /* safe: sig_atomic_t, volatile */
}
/* ── register keyword ─────────────────────────────────────
'register int i' - hint to use CPU register; compiler ignores
it in modern compilers. Cannot take address of register var. */
/* ── Summary table ──────────────────────────────────────────
Storage class Location Lifetime Linkage
───────────── ────────── ───────── ──────────
auto (default) Stack Function None
static (local) BSS/Data Program None (local scope)
static (file) BSS/Data Program Internal
extern BSS/Data Program External
register Register(?) Function None
volatile N/A Any depends depends
(qualifier, not storage class) */
int main(void) {
/* auto - always the default for local variables */
auto int x = 5; /* identical to: int x = 5; */
printf("x = %d
", x);
/* Signal handler registration */
signal(SIGINT, sigint_handler);
printf("Press Ctrl+C to set stop_flag
");
/* In a real program: while (!stop_flag) { do_work(); } */
hw_status_reg = 1; /* simulate hardware becoming ready */
wait_for_hardware(); /* will immediately proceed */
increment_shared();
printf("shared_counter = %d
", shared_counter);
return 0;
}Common Mistakes
- Using a non-volatile flag in a signal handler - the compiler may cache the flag in a register. The signal handler's write to the register copy doesn't affect the cached value in the main loop. Always: `volatile sig_atomic_t flag` for signal handler communication.
- Thinking `volatile` provides thread safety - `volatile` prevents compiler optimisation. It does NOT prevent out-of-order execution on the CPU (a CPU memory model issue). For thread synchronisation, use `_Atomic` (C11) or `pthread` mutexes.
- `extern` without a definition - `extern int x;` in every file without ONE file having `int x;` = linker error: undefined reference. Exactly one `.c` file must actually define the variable.
Tip
Tip
Practice Storage Classes auto register extern static volatile in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Storage classes specify variable lifetime, scope visibility, and compiler optimization behavior
Practice Task
Note
Practice Task - (1) Write a working example of Storage Classes auto register extern static volatile from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Storage Classes auto register extern static volatile is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- Every C variable has a **storage class** that determines its lifetime, linkage, and storage location.
- Using a non-volatile flag in a signal handler - the compiler may cache the flag in a register. The signal handler's write to the register copy doesn't affect the cached value in the main loop. Always: `volatile sig_atomic_t flag` for signal handler communication.
- Thinking `volatile` provides thread safety - `volatile` prevents compiler optimisation. It does NOT prevent out-of-order execution on the CPU (a CPU memory model issue). For thread synchronisation, use `_Atomic` (C11) or `pthread` mutexes.
- `extern` without a definition - `extern int x;` in every file without ONE file having `int x;` = linker error: undefined reference. Exactly one `.c` file must actually define the variable.
Finished this lesson?