- The five storage classes in C —
auto,register,static,extern, andtypedef - How each storage class affects scope, visibility, lifetime, and default initialization
- When to use each class for performance, state persistence, and modular design
- How storage classes interact with recursion, nested calls, and multi-file projects
- Writing clean, well-scoped C code using the right storage class for the job
What are Storage Classes?
A storage class in C defines four properties of a variable: its scope, its visibility, its lifetime, and its default value. C provides five storage-class specifiers:
| Specifier | Default value | Lifetime |
|---|---|---|
auto | Garbage | Block scope |
register | Garbage | Block scope (stored in CPU register if possible) |
static | Zero | Program lifetime |
extern | Zero | Program lifetime (declared elsewhere) |
typedef | — | Compile-time alias (not a storage location) |
💡 Quick Definition: A storage class tells the compiler where to store a variable, how long it should live, and who can see it.
The 12 Practice Assignments
Each assignment below is a full working problem, explained step-by-step with a complete C program and output. Work through them in order — the difficulty increases gradually.
How to Approach Each Assignment
Every storage-class program follows the same pattern. Once you recognize it, the assignments become predictable:
The Universal Storage-Class Skeleton
#include <stdio.h>
/* Global variable if needed */
int globalVar = 100;
void demoFunction(void) {
/* Declare the variable with the storage class under test */
static int callCount = 0; /* or: int x; or: register int i; */
callCount++;
printf("Called %d time(s)\n", callCount);
}
int main(void) {
demoFunction();
demoFunction();
demoFunction();
return 0;
}
5 checkpoints for every assignment:
- Know the default.
autoandregisterstart as garbage;staticandexternstart as zero. - Know the lifetime.
staticandexternlive for the whole program;autoandregisterdie at block exit. - Know the scope.
externis visible across files; the rest are file- or block-local. registeris a request, not a command. The compiler may ignore it — modern optimizers usually make the choice themselves.typedefis not a storage class. It creates a type alias at compile time — no memory involved.
🎯 Key Takeaway: Picking the right storage class is a design decision. Get it wrong and you get bugs that are hard to trace — stale state, unexpected garbage values, or globals conflicting across files. Get it right and your code is smaller, faster, and easier to reason about.
Frequently Asked Questions About Storage Classes
1. What is the default storage class for a local variable in C?
auto. Any variable declared inside a block without an explicit storage-class specifier is auto by default — stored on the stack, uninitialized until you assign a value.
2. Does register actually put a variable in a CPU register?
Only if the compiler decides to. register is a hint — the compiler can ignore it, especially when the variable's address is taken or there aren't enough registers. In modern C code, the compiler's optimizer usually does a better job than manual register hints.
3. What is the difference between static local and static global?
A static local variable keeps its value between function calls but is visible only inside the function. A static global variable has file scope — visible to all functions in that file, but not to other files that extern-declare it.
4. Can I declare an extern variable without a definition somewhere?
Yes, but only if some translation unit in the program defines it. Otherwise, the linker will report an "undefined reference" error. extern is a declaration ("it exists somewhere else"), not a definition.
5. Why is typedef listed with storage classes if it isn't one?
Because in C's grammar and many textbooks, typedef is grouped with the storage-class specifiers syntactically. Conceptually it's a type-alias construct, not a memory-location modifier — but it appears in the same place in a declaration.
💡 Tip: Use nm or objdump -t on your compiled object files to see where each variable ended up — in .data, .bss, .text, or on the stack.