Storage Classes in C

    Atul Kabra3 min readUpdated

    A storage class in C controls three things about a variable: its scope (where it's visible), its lifetime (how long it lives in memory), and its linkage (whether it can be shared across files). The four storage class keywords are auto, static, extern, and register. Getting these right explains why a variable "remembers" its value — or doesn't.

    auto: the default for locals

    Every local variable is auto by default, so you rarely write the keyword. An auto variable lives only while its block runs, and is re-created each time:

    void demo(void) {
        auto int count = 0;   /* same as: int count = 0; */
        count++;              /* always 1 here -- reset on every call */
    }
    

    Its scope is the block it's declared in; its lifetime ends when the block exits.

    static: remembers between calls

    A static local variable keeps its value between function calls. It is initialized once and persists for the whole program:

    #include <stdio.h>
    
    void counter(void) {
        static int calls = 0;   /* initialized once, retained after */
        calls++;
        printf("Called %d time(s)\n", calls);
    }
    
    int main(void) {
        counter();   /* Called 1 time(s) */
        counter();   /* Called 2 time(s) */
        counter();   /* Called 3 time(s) */
        return 0;
    }
    

    static has another use at file level: a static global variable or function is visible only within its own source file — a way to keep helpers private.

    extern: sharing across files

    extern declares that a variable is defined elsewhere (usually in another file), letting you share one global across multiple source files:

    /* file1.c */
    int total = 0;          /* the actual definition */
    
    /* file2.c */
    extern int total;       /* "this exists somewhere else" */
    

    extern says "trust me, the linker will find it." Without it, each file would think it owns a separate variable.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming

    register: a hint (largely historical)

    register suggests the compiler keep a variable in a CPU register for speed. Modern compilers optimize so well that this hint is mostly ignored, and you cannot take the address of a register variable with &:

    register int i;   /* a hint only -- the compiler may ignore it */
    

    You'll rarely need it today; it's worth knowing for older code and exams.

    Scope and lifetime summary

    Storage classScopeLifetime
    auto (default local)blockblock execution
    static (local)blockwhole program
    static (global/function)file onlywhole program
    externacross fileswhole program
    registerblockblock execution

    Common mistakes

    • Expecting an auto local to remember its value between calls — it resets each time. Use static for memory.
    • Defining a global in a header and including it in many files — that creates duplicate definitions. Define once in a .c file, declare with extern in the header.
    • Taking the address of a register variable — not allowed.
    • Confusing the two meanings of static: inside a function it means "persistent lifetime"; at file scope it means "private to this file."
    • Relying on uninitialized auto variables — they hold garbage; static variables default to zero.

    FAQ

    Why does my counter reset to 1 every call? It's an auto local. Make it static to keep its value.

    Is register still useful? Almost never — trust the optimizer. Know it for legacy code.

    Continue at the hub C Programming, then Functions in C and Variables & Data Types.

    Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides