The 4 Storage Classes in C: auto, register, static & extern
Unit 4: Structures, Unions, Enums & Storage Classes • Problem Solving Using C
👨🏫 Professor's Mental Model: Sticky Note vs CPU Pocket Tool vs Permanent Blackboard vs Public Notice Board
Storage Class decide karti hai ki variable kahan rahega aur kab tak zinda rahega! 'auto' ek temporary sticky note hai jo meeting (function) khatam hote hi dustbin me fek diya jata hai. 'register' carpenter ki jeb me rakhe screwdriver jaisa hai: direct CPU ke paas ultra-fast speed me! 'static' blackboard par permanent chalk se likha number hai: classroom khali ho jaye tab bhi number wahi rehta hai (value retain)! Aur 'extern' college ke main notice board jaisa hai jise kisi bhi department ke students dekh sakte hain (Global)!
Academic Lecture Notes & Solved Study Pages
Unit 4 • Core Concepts, Step-by-Step Proofs & Notebook Solutions
1. The 4 Determinant Attributes of Every Variable
Every variable in C is characterized by 4 structural properties: 1. Storage Location: Where in hardware the variable is stored (RAM Stack, CPU Register, or Data Segment). 2. Default Initial Value: What value it holds if uninitialized (Garbage vs Zero). 3. Scope: Where in the source code the variable is visible and accessible (Block/Local vs Global/File). 4. Lifetime (Longevity): How long the variable stays alive in memory (until block exit vs until program termination).
2. Comprehensive Breakdown of the 4 Storage Classes
3. Static Local vs Static Global Variables
| Storage Class | Hardware Location | Default Value | Scope (Visibility) | Lifetime (Duration) |
|---|---|---|---|---|
| auto | RAM (Stack) | Garbage Value | Local to enclosing block | Until block terminates |
| register | CPU Register | Garbage Value | Local to enclosing block | Until block terminates |
| static | RAM (Data Segment) | Strictly Zero (0) | Local to declaring block | Entire program execution lifespan |
| extern | RAM (Data Segment) | Strictly Zero (0) | Global across all project C files | Entire program execution lifespan |
Interactive Tested Code Example
#include <stdio.h>
void hit_counter(void) {
auto int regular_var = 0; // Re-initialized to 0 on EVERY call!
static int static_var = 0; // Initialized ONLY ONCE at program start!
regular_var++;
static_var++;
printf("auto regular_var: %d | static static_var: %d\n", regular_var, static_var);
}
int main(void) {
printf("=== STORAGE CLASS PERSISTENCE DEMO ===\n");
printf("Call 1: "); hit_counter();
printf("Call 2: "); hit_counter();
printf("Call 3: "); hit_counter();
return 0;
}🎯 University Exam Scoring Blueprint
- In university exams, the 4-Storage Classes comparison table is a mandatory scoring diagram.
- Highlight why & cannot be used on a register variable (registers have no RAM memory address).
- Explain that static variables are initialized only once at program startup in the data segment.