Storage Classes: A First Look
What auto, static, extern, and register mean on a declaration, previewed here because exam papers ask early; the full story arrives with functions.
Four Keywords About Where Variables Live
Among the 32 keywords are four that answer a different question than type does. Type says how big a variable is and how to read its bits; a storage class says where the variable lives, how long it survives, and who can see it: auto, static, extern, and register. Exam papers ask for the list and a one-line meaning of each remarkably early, so this lesson gives you exactly that, honestly labeled as a preview. The deep version needs functions, and arrives with them in chapter 9.
auto: The Default You Never Write
Every variable you have declared so far, inside main, at the top of the block, is an automatic variable: it comes into existence when its block starts and vanishes when the block ends. The keyword auto states this explicitly:
auto int count = 0; /* legal, and completely redundant */
int count = 0; /* identical meaning */
Because block-scope variables are automatic by default, real code never writes auto. It survives as an exam question ("what is the default storage class of a local variable?") and as a historical curiosity. (If you later meet C++, its auto keyword means something entirely different; another reason nobody mourns the C one.)
static: A Variable That Survives
A static variable is the opposite of automatic in one specific way: it is created once, before the program starts, and survives until the program ends, keeping its value between visits to its block.
static int callCount = 0;
Where an automatic variable is a fresh start every time, a static one is a running tally. A second, separate use of the same keyword controls whether a name is visible to other source files. Both faces of static only become demonstrable once programs have functions to re-enter and files to share, so chapter 9 owns the working examples; for now, the definition to hold: static means the value persists for the whole program run.
One more fact worth having early: static variables without an initializer start at zero, unlike automatic variables, which start indeterminate. The exam phrasing is "static variables are initialized to zero by default; automatic variables are not initialized".
extern: Declared Here, Defined Elsewhere
extern says: this variable exists, but its storage is created somewhere else, typically in another source file.
extern int sharedTotal; /* a promise: sharedTotal is defined elsewhere */
It is how one file uses a variable that another file owns. Since every course program so far is a single file with a single function, there is nowhere else for a variable to live yet; the multi-file chapter makes extern real. Definition to hold: extern declares without creating, deferring to a definition elsewhere.
register: A Polite, Obsolete Request
register asks the compiler to keep a variable in a CPU register rather than memory, for speed:
register int i;
Modern compilers make this decision themselves, far better than humans, and simply ignore the hint. The keyword's one real effect is that taking the address of a register variable is an error, since a register has no address. It appears on exams and in old code; write it in neither.
The Table Exams Want
| Storage class | Lifetime | Default initial value | One-line meaning |
|---|---|---|---|
auto |
its block | indeterminate | the default for locals; fresh each entry |
static |
whole program | zero | persists between visits |
extern |
whole program | zero (at its definition) | defined in another file |
register |
its block | indeterminate | speed hint; ignored today; no address |
If an exam asks "how many storage classes does C have?", the expected answer is these four.
Key Takeaways
- Storage class answers lifetime and visibility; type answers size and interpretation.
- Local variables are
autoby default; the keyword itself is never needed. staticvariables live for the whole program, keep their value between visits, and default to zero.externdeclares a variable whose storage is defined elsewhere, enabling sharing across files.registeris a historical speed hint compilers ignore; its only teeth is that such a variable has no address.- The working demonstrations need functions and multiple files: chapter 9 completes this story.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Storage Classes: A First Look - Quiz
Test your understanding of the lesson.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!