NBC 103 • Unit 416 min readHigh Exam Frequency

The 4 Storage Classes in C: auto, register, static & extern

Unit 4: Structures, Unions, Enums & Storage ClassesProblem 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

3 Notebook Pages
NOTEBOOK PAGE 1 OF 3

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).

NOTEBOOK PAGE 2 OF 3

2. Comprehensive Breakdown of the 4 Storage Classes

1. auto (Automatic - Default for local variables):
- Storage: Stack segment in RAM.
- Default Value: Unpredictable Garbage value.
- Scope: Local to the enclosing block `{}`.
- Lifetime: Created upon block entry, destroyed upon block exit.
2. register:
- Storage: Microprocessor CPU Register (not RAM!).
- Default Value: Garbage value.
- Scope: Local block scope.
- Lifetime: Block duration.
- Crucial Limitation: The address-of operator (`&`) CANNOT be applied to register variables because CPU registers do not have memory RAM addresses!
3. static (Persistent State):
- Storage: Permanent Data Segment in RAM.
- Default Value: Automatically initialized to ZERO (0)!
- Scope: Local to the block where declared.
- Lifetime: Persists throughout the ENTIRE PROGRAM duration! It retains its updated value between multiple consecutive function calls.
4. extern (External / Global Linkage):
- Storage: Data Segment in RAM.
- Default Value: Zero.
- Scope: Global / Multi-file scope. Declares a variable that is defined in another source file or external translation unit.
NOTEBOOK PAGE 3 OF 3

3. Static Local vs Static Global Variables

Static Local Variable: Declared inside a function. Accessible only inside that function, but retains its state across multiple invocations.
Static Global Variable: Declared outside functions with `static`. Restricts variable visibility to THAT SOURCE FILE ONLY (Private translation unit), preventing naming collisions across multi-developer C projects.
Master 4 Storage Classes Specification Matrix
Storage ClassHardware LocationDefault ValueScope (Visibility)Lifetime (Duration)
autoRAM (Stack)Garbage ValueLocal to enclosing blockUntil block terminates
registerCPU RegisterGarbage ValueLocal to enclosing blockUntil block terminates
staticRAM (Data Segment)Strictly Zero (0)Local to declaring blockEntire program execution lifespan
externRAM (Data Segment)Strictly Zero (0)Global across all project C filesEntire 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;
}
💡 Note:Demonstrates that regular auto variables are recreated and destroyed on every call, while static variables persist their accumulated state throughout program life.

🎯 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.

Top Viva Questions on The 4 Storage Classes in C: auto, register, static & extern

2 Questions
1

Why does the compiler ignore the 'register' keyword sometimes?

2

What is the difference between declaring a variable with extern versus defining it?