Storage Classes
Scope and lifetime: the two big ideas
Every variable in C has two properties beyond its value: scope (where it can be used) and lifetime (how long it survives in memory). Storage classes are simply the four keywords that control these two things.
This lesson builds the intuition with everyday examples. For a compact reference-style version, see storage classes in C as well.
| Idea | Question it answers |
|---|---|
| Scope | Where can I use this variable? |
| Lifetime | How long does it keep its value? |
auto: the everyday default
Every ordinary local variable is auto — you never have to type the word. It is created when its block runs and gone when the block ends.
void demo() {
int x = 5; // this is auto by default
// 'auto int x = 5;' means exactly the same
} // x disappears hereregister: a speed hint
register asks the compiler to keep a variable in a fast CPU register if it can — useful for a heavily used loop counter. It is only a hint; modern compilers usually decide better on their own.
for (register int i = 0; i < 1000; i++) {
// i may be kept in a CPU register for speed
}Because a register variable might not live in normal memory, you cannot use & on it.
static: memory that remembers
A static local variable is created only once and keeps its value between calls — perfect for counting how many times a function ran.
#include <stdio.h>
void counter() {
static int count = 0; // created once, remembered
count++;
printf("Called %d times\n", count);
}
int main() {
counter(); counter(); counter();
return 0;
}Called 1 times
Called 2 times
Called 3 times
Without static, count would reset to 0 every call and always print 1.
extern: sharing across files
extern lets several source files use the same global variable. You define it once, and declare it with extern wherever else you need it.
// file1.c int total = 100; // the real definition // file2.c extern int total; // "it lives in another file" // now file2 can use total
All four at a glance
| Class | Scope | Lifetime | Typical use |
|---|---|---|---|
auto | Local block | Until block ends | Ordinary local variables |
register | Local block | Until block ends | Fast loop counters (hint) |
static | Local or file | Whole program | Remembering state; hiding a file's globals |
extern | All files | Whole program | Sharing a global across files |
Common mistakes
- Expecting an ordinary local to remember its value — you need
static. - Using
&on aregistervariable (not allowed). - Defining an
externvariable in two files — define once, declare elsewhere. - Confusing
static's two roles: remembering state (local) vs hiding (file level).
Write a function with a static counter that prints how many times it has run, and call it in a loop five times. Then remove static and watch the output change to all 1s.
Summary
- Storage classes set a variable's scope and lifetime.
autois the default for local variables (block scope, short life).registerhints at keeping a variable in a fast register.staticremembers a value between calls, or hides a file's globals.externshares one global variable across multiple files.
Frequently Asked Questions
What is a storage class in C?
auto, register, static and extern — each setting these differently.What is the difference between scope and lifetime?
What does the static keyword do in C?
static makes a local variable keep its value between calls instead of being created fresh each time, so it can remember state such as a call count. At file level, static limits a variable or function to that one file, hiding it from others.What is the default storage class in C?
auto by default, meaning they are created when the block runs and destroyed when it ends, and are visible only inside that block. You almost never write the auto keyword because it is already the default.What is the extern storage class used for?
extern declares that a variable is defined in another file, letting several source files share one global variable. You define the variable once without extern, and use extern in the other files to refer to that same variable.