Storage Classes in C
Every variable has four properties: where it is stored, its default value, where it can be used, and how long it lives.
A storage class decides all four. C has four storage classes: auto, register, static and extern. The comparison table in this lesson is one of the most asked questions in C exams.
What You Will Learn
- What a storage class is.
- The four storage classes: auto, register, static, extern.
- The comparison table: storage, default value, scope, lifetime.
- How a
staticvariable remembers its value. - How
externuses a variable defined somewhere else.
Storage Classes in C Explained
Exam definition: A storage class defines the storage location, default initial value, scope and lifetime of a variable. C has four storage classes: auto, register, static and extern.
The comparison table (learn this!)
| Storage class | Stored in | Default value | Scope | Lifetime |
|---|---|---|---|---|
auto | Memory (stack) | Garbage | Local (block) | Till the block ends |
register | CPU register (if available) | Garbage | Local (block) | Till the block ends |
static | Memory (data segment) | 0 | Local (block), or file if global | Whole program |
extern | Memory (data segment) | 0 | Global (whole program, all files) | Whole program |
Memory trick: "A R S E" = auto, register, static, extern. The first two (A, R) are short-lived with garbage; the last two (S, E) live forever with 0.
1. auto
- The default for local variables.
int x;inside a function is the same asauto int x;. - Created when the block starts, destroyed when it ends.
2. register
- A request to keep the variable in a fast CPU register (good for loop counters).
- The compiler may ignore the request. Modern compilers optimise this automatically.
- You cannot take its address with
&.
3. static
- A static local variable is created once and keeps its value between function calls.
- It is initialized only one time. If you don't give a value, it starts at 0.
- A static global variable or function can be used only inside its own file.
4. extern
- Tells the compiler: "this variable is defined somewhere else (later in this file or in another file), don't create a new one".
- Used to share one global variable between many files.
extern int total;is a declaration only; no memory is reserved.
Syntax of Storage Classes
storage_class data_type variable_name;
auto int a = 5;
register int i;
static int count = 0;
extern int total;
Example 1: static vs auto Inside a Function
#include <stdio.h>
void counter(void)
{
auto int a = 0; /* created fresh on every call */
static int s = 0; /* created once, keeps its value */
a = a + 1;
s = s + 1;
printf("auto a = %d, static s = %d\n", a, s);
}
int main(void)
{
counter();
counter();
counter();
return 0;
}
Output
auto a = 1, static s = 1 auto a = 1, static s = 2 auto a = 1, static s = 3
Code Explanation
auto int a = 0;: every timecounter()runs, a newais made and set to 0. Aftera + 1, it is always 1.static int s = 0;: this line runs only once, the first time. After that,skeeps its value.- Call 1: s becomes 1. Call 2: s becomes 2. Call 3: s becomes 3.
sis still local:main()cannot use it directly. Only its lifetime is the whole program.
How Storage Classes Work in Memory
- auto variables live on the stack, which is cleaned up when a function returns.
- register variables may be kept inside the CPU itself, which is the fastest place.
- static and extern (global) variables live in the data segment, which exists from the start to the end of the program and is filled with zeros at the start.
Example 2: Using extern to Access a Variable Defined Later
#include <stdio.h>
extern int total; /* declaration: "total exists somewhere" */
int main(void)
{
printf("Total = %d\n", total);
return 0;
}
int total = 100; /* definition: memory is created here */
Output
Total = 100
Without the extern line, main() would not know about total, because it is defined below main(). In bigger programs, the definition is usually in another .c file.
Important Points to Remember
- 4 storage classes: auto, register, static, extern.
autois the default for local variables.staticlocal variables keep their value between calls.staticandexterndefault to 0; auto and register default to garbage.externdeclares a variable without creating it.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
| Thinking static makes a variable global | A static local is still only usable in its function | Static changes lifetime, not scope |
&i on a register variable | Compile error | Don't use register if you need the address |
extern int x; with no definition anywhere | Linker error: undefined reference to 'x' | Define it once: int x; |
extern int x = 5; inside a function | Error | Initialize at the definition, not at an extern declaration |
Storage Classes Practice Questions
- What is a storage class? Name the four storage classes.
- Write the storage, default value, scope and lifetime of a
staticvariable. - What is the difference between
autoandstatic? - Why can't we use
&with aregistervariable? - What does
externdo?
Show answers
- It defines the storage location, default value, scope and lifetime of a variable. auto, register, static, extern.
- Memory (data segment), default 0, local scope (or file scope if global), lifetime is the whole program.
- An auto variable is created and destroyed on every call and starts with garbage; a static variable is created once, keeps its value between calls and starts with 0.
- Because it may be stored in a CPU register, which has no memory address.
- It declares a variable that is defined elsewhere, so it can be used without creating a new one.
Coding Exercise
Write a function next_token() that returns a new token number every time it is called (101, 102, 103, ...) using a static variable. Call it 3 times from main().
Show solution
#include <stdio.h>
int next_token(void)
{
static int token = 100;
token = token + 1;
return token;
}
int main(void)
{
printf("Token: %d\n", next_token());
printf("Token: %d\n", next_token());
printf("Token: %d\n", next_token());
return 0;
}
Token: 101 Token: 102 Token: 103
Quick Quiz: Storage Classes in C (MCQ)
-
Which is the default storage class of a local variable?
A) static B) extern C) auto D) register
-
A static local variable keeps its value:
A) Only once B) Between function calls C) Never D) Only in main()
-
The default value of a static variable is:
A) Garbage B) 1 C) 0 D) Undefined
-
Which storage class may store a variable in a CPU register?
A) auto B) register C) static D) extern
-
extern int x;is a:A) Definition B) Declaration C) Function D) Error
Show answers
- C: auto.
- B: between function calls.
- C: 0.
- B: register.
- B: a declaration (no memory is created).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| 4 storage classes | auto, register, static, extern |
|---|---|
| auto | Stack, garbage, local, till block ends |
| register | CPU register, garbage, local, till block ends |
| static | Data segment, 0, local, whole program |
| extern | Data segment, 0, global, whole program |
| Default for locals | auto |
| Keeps value between calls | static |
Frequently asked questions
- What is the use of a static variable?
- It keeps its value between function calls, because it lives for the whole program.
- Does static change the scope of a local variable?
- No. It changes only the lifetime; the scope stays local.
- What is the difference between extern declaration and definition?
extern int x;only declares it;int x;defines it and creates memory.- Is register always stored in a register?
- No. It is only a request; the compiler may ignore it.
Remember
- Order: A-R-S-E.
- A, R = short life, garbage. S, E = whole program, 0.
Next Topic
Next lesson: printf() Function in C. Module 03 starts with the most-used function in C: printf().