C Programming - Beginner to Brilliance / Functions in C

Storage Classes

Storage classes in C define four key things about a variable:

🧠 Scope – Where the variable is accessible

🧬 Lifetime – How long it exists in memory

🔍 Visibility – Who can access it

🧰 Default initial value – If you don’t initialize it, what value will it get?


🎯 Why Do We Need Storage Classes?

Let’s say you work in a shared kitchen. You can:

  • Keep ingredients in your locker (private, persistent).

  • Keep them on the counter (temporary use).

  • Use shared pantry ingredients (accessible to everyone).

  • Mark something for quick access (fast storage).

This is exactly what storage classes do for variables in your code.


📑 The 4 Storage Classes in C:

Storage Class

Keyword

Stored In

Default Value

Scope

Lifetime

Automatic

auto

Stack

Garbage value

Local to block

Till block ends

External

extern

Global Memory

Zero

Global

Till program ends

Static

static

Data Segment

Zero

Local/Global

Till program ends

Register

register

CPU Register*

Garbage value

Local

Till block ends


🔹 auto – Automatic Storage Class

🔍 Characteristics:

  • Default for local variables.

  • Allocated in stack memory.

  • Created when the block starts, destroyed when it ends.

void show() {
    auto int a = 10;
    printf("%d", a);
}

🧠 Even if you don’t write auto, it's automatically considered.


🔹 extern – External Storage Class

📌 Used to declare global variables that are defined in another file or scope.

// File1.c
int x = 5;

// File2.c
extern int x;
printf("%d", x); // Accesses variable from File1

🔍 Characteristics:

  • Used for sharing variables between files.

  • Does not allocate memory (only declaration).

  • Memory is shared with the original definition.

🧠 Think of it as "I want to borrow this variable from someone else!"


🔹 static – Static Storage Class

🧠 Very important keyword.

2 Major Uses:

  1. Inside a function – Keeps its value even after function ends

  2. Outside functions – Makes a global variable private to the file

🧪 Example: Inside function

void counter() {
    static int count = 0;
    count++;
    printf("%d\\n", count);
}

int main() {
    counter(); // 1
    counter(); // 2
    counter(); // 3
}
1
2
3

If you used int count = 0; instead of static, output would be 1 every time.

🧪 Example: Outside function (file-private)

static int secret = 42; // Can’t be accessed from other files

🧠 Think of static as a variable that has memory and loyalty – it remembers you always and stays only with you!


🔹 register – Fast Storage (Register Memory)

A register is a small, ultra-fast memory unit inside the CPU itself. It's the fastest storage the processor can access — even faster than cache or RAM.

📦 Used to store variables in CPU registers instead of RAM (for fast access).

void quickAccess() {
    register int speed = 10;
    printf("%d", speed);
}

🧠 Key Points:

  • Stored in CPU registers (if available).

  • You cannot get the address of register variables (no &speed).

  • Good for loop counters, frequently used variables.

⚠️ Modern compilers may ignore this hint and optimize as needed.


🔍 Comparison Table:

Keyword

Memory Location

Default Value

Accessible using &?

Persists after block ends?

auto

Stack

Garbage

✅ Yes

❌ No

extern

Global

Zero (if not initialized)

✅ Yes

✅ Yes

static

Data Segment

Zero (if not initialized)

✅ Yes

✅ Yes

register

CPU Register

Garbage

❌ No

❌ No


🧠 Real Life Analogy:

Storage Class

Real-life Analogy

auto

Notepad you use during a single meeting

static

Whiteboard in your room (keeps content even after leaving and re-entering)

extern

Shared Google Doc someone else created

register

Sticky note stuck on your computer (for fast access, but you can’t take it with you)