C Storage Classes

C Storage Classes
Storage classes in C define the scope, lifetime, and visibility of variables and functions within a program.
They tell the compiler:
✔ Where the variable is stored
✔ How long the variable exists (lifetime)
✔ Who can access it (scope)
✔ What is its default value
🔹 Available Storage Classes in C
| Storage Class | Scope | Lifetime | Default Value | Memory Location |
|---|---|---|---|---|
auto | Local (block) | Function/block duration | Garbage value | Stack |
register | Local | Function/block duration | Garbage value | CPU register (if available) |
static | Local or Global | Entire program duration | Zero | Memory (Static/Data Segment) |
extern | Global | Entire program duration | Zero | Memory (Static/Data Segment) |
1️⃣ auto (Automatic Storage)
Default for local variables
Exists only inside functions or blocks
Stored in stack memory
✔ Usually we don’t use auto keyword explicitly because it’s default.
2️⃣ register
Suggests storing variable in CPU register for faster access
Cannot take its memory address using
&
❗ Note: Compiler may ignore this request.
3️⃣ static
A static variable preserves its value between function calls.
✔ Example: Local static variable
Output:
🔹 Unlike normal local variables, static does not reset.
✔ Static Global Variable
A static global variable is accessible only inside the file, not from other .c files.
4️⃣ extern
extern is used to declare a variable that is defined in another file or location.
Useful in multi-file projects.
📌 Example
📁 file1.c
📁 file2.c
✔ extern does not allocate memory — it only references existing storage.
🧠 Summary Table
| Keyword | Use Case |
|---|---|
auto | Default local variable |
register | Faster access storage suggestion |
static | Persistent variable or file-level global variable |
extern | Global variable sharing between files |
