</>
ShikshaCSLearn. Code. Grow.
🔍
☕ Support Us
ShikshaCSNotesProgramming in C
Programming in C
🕒 7 min read

Storage Classes (auto, static, extern, register)

Controlling a variable's lifetime and visibility across your program.

auto (Default)

Every local variable is 'auto' by default — it's created when its block starts and destroyed when the block ends. You almost never need to write 'auto' explicitly; it's just the normal behavior of local variables.

static

A static local variable keeps its value between function calls instead of resetting each time — it's initialized only once, and its value persists across multiple calls to the same function. A static global variable, in contrast, limits that variable's visibility to only the current file.

void counter() {
  static int count = 0; // initialized only ONCE
  count++;
  printf("%d\n", count);
}
// calling counter() three times prints 1, 2, 3

extern & register

extern declares that a variable is defined in another file, letting multiple files share one global variable. register suggests (not guarantees) that a variable be stored in a CPU register instead of RAM for faster access — modern compilers mostly optimize this automatically.

🌍 Real-World Use

A function that needs to count how many times it's been called (like tracking total logins in a session) uses a static variable to remember its value between calls, without needing a global variable.

💡 Pro Tip

The most commonly tested storage class in exams/interviews is 'static' — always be ready to explain both its uses: (1) static local variable retains value across calls, (2) static global variable/function limits visibility to its own file.

🧪 Quick Self-Test

Check what you just learned — no pressure, just practice.

1. What happens to a static local variable's value between function calls?

2. What does 'extern' allow you to do?

← Back to all Notes