🟡 Intermediate  ·  Lesson 29

Storage Classes

Scope and lifetime: the two big ideas

Every variable in C has two properties beyond its value: scope (where it can be used) and lifetime (how long it survives in memory). Storage classes are simply the four keywords that control these two things.

This lesson builds the intuition with everyday examples. For a compact reference-style version, see storage classes in C as well.

IdeaQuestion it answers
ScopeWhere can I use this variable?
LifetimeHow long does it keep its value?

auto: the everyday default

Every ordinary local variable is auto — you never have to type the word. It is created when its block runs and gone when the block ends.

C Language
void demo() {
    int x = 5;          // this is auto by default
    // 'auto int x = 5;' means exactly the same
}                        // x disappears here

register: a speed hint

register asks the compiler to keep a variable in a fast CPU register if it can — useful for a heavily used loop counter. It is only a hint; modern compilers usually decide better on their own.

C Language
for (register int i = 0; i < 1000; i++) {
    // i may be kept in a CPU register for speed
}
💡 You cannot take its address

Because a register variable might not live in normal memory, you cannot use & on it.

static: memory that remembers

A static local variable is created only once and keeps its value between calls — perfect for counting how many times a function ran.

C Language
#include <stdio.h>
void counter() {
    static int count = 0;   // created once, remembered
    count++;
    printf("Called %d times\n", count);
}
int main() {
    counter(); counter(); counter();
    return 0;
}
Output:
Called 1 times
Called 2 times
Called 3 times

Without static, count would reset to 0 every call and always print 1.

extern: sharing across files

extern lets several source files use the same global variable. You define it once, and declare it with extern wherever else you need it.

C Language
// file1.c
int total = 100;          // the real definition

// file2.c
extern int total;         // "it lives in another file"
// now file2 can use total

All four at a glance

ClassScopeLifetimeTypical use
autoLocal blockUntil block endsOrdinary local variables
registerLocal blockUntil block endsFast loop counters (hint)
staticLocal or fileWhole programRemembering state; hiding a file's globals
externAll filesWhole programSharing a global across files

Common mistakes

  • Expecting an ordinary local to remember its value — you need static.
  • Using & on a register variable (not allowed).
  • Defining an extern variable in two files — define once, declare elsewhere.
  • Confusing static's two roles: remembering state (local) vs hiding (file level).
🏋️ Practice

Write a function with a static counter that prints how many times it has run, and call it in a loop five times. Then remove static and watch the output change to all 1s.

Summary

  • Storage classes set a variable's scope and lifetime.
  • auto is the default for local variables (block scope, short life).
  • register hints at keeping a variable in a fast register.
  • static remembers a value between calls, or hides a file's globals.
  • extern shares one global variable across multiple files.

Frequently Asked Questions

What is a storage class in C?
A storage class tells the compiler two things about a variable: its scope (where in the program it can be used) and its lifetime (how long it keeps its value in memory). C has four — auto, register, static and extern — each setting these differently.
What is the difference between scope and lifetime?
Scope is where a variable is visible — for example only inside the function that declares it. Lifetime is how long it exists in memory — for example until the function returns, or for the whole program run. Storage classes control both.
What does the static keyword do in C?
Inside a function, static makes a local variable keep its value between calls instead of being created fresh each time, so it can remember state such as a call count. At file level, static limits a variable or function to that one file, hiding it from others.
What is the default storage class in C?
Local variables are auto by default, meaning they are created when the block runs and destroyed when it ends, and are visible only inside that block. You almost never write the auto keyword because it is already the default.
What is the extern storage class used for?
extern declares that a variable is defined in another file, letting several source files share one global variable. You define the variable once without extern, and use extern in the other files to refer to that same variable.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.