Skip to content
StudyFame

Storage Classes in C

Every variable has four properties: where it is stored, its default value, where it can be used, and how long it lives.

A storage class decides all four. C has four storage classes: auto, register, static and extern. The comparison table in this lesson is one of the most asked questions in C exams.

What You Will Learn

  • What a storage class is.
  • The four storage classes: auto, register, static, extern.
  • The comparison table: storage, default value, scope, lifetime.
  • How a static variable remembers its value.
  • How extern uses a variable defined somewhere else.

Storage Classes in C Explained

Exam definition: A storage class defines the storage location, default initial value, scope and lifetime of a variable. C has four storage classes: auto, register, static and extern.

The comparison table (learn this!)

Storage classStored inDefault valueScopeLifetime
autoMemory (stack)GarbageLocal (block)Till the block ends
registerCPU register (if available)GarbageLocal (block)Till the block ends
staticMemory (data segment)0Local (block), or file if globalWhole program
externMemory (data segment)0Global (whole program, all files)Whole program

Memory trick: "A R S E" = auto, register, static, extern. The first two (A, R) are short-lived with garbage; the last two (S, E) live forever with 0.

1. auto

  • The default for local variables. int x; inside a function is the same as auto int x;.
  • Created when the block starts, destroyed when it ends.

2. register

  • A request to keep the variable in a fast CPU register (good for loop counters).
  • The compiler may ignore the request. Modern compilers optimise this automatically.
  • You cannot take its address with &.

3. static

  • A static local variable is created once and keeps its value between function calls.
  • It is initialized only one time. If you don't give a value, it starts at 0.
  • A static global variable or function can be used only inside its own file.

4. extern

  • Tells the compiler: "this variable is defined somewhere else (later in this file or in another file), don't create a new one".
  • Used to share one global variable between many files.
  • extern int total; is a declaration only; no memory is reserved.

Syntax of Storage Classes

storage_class data_type variable_name;

auto int a = 5;
register int i;
static int count = 0;
extern int total;

Example 1: static vs auto Inside a Function

#include <stdio.h>

void counter(void)
{
    auto int a = 0;      /* created fresh on every call */
    static int s = 0;    /* created once, keeps its value */

    a = a + 1;
    s = s + 1;
    printf("auto a = %d, static s = %d\n", a, s);
}

int main(void)
{
    counter();
    counter();
    counter();
    return 0;
}

Output

auto a = 1, static s = 1
auto a = 1, static s = 2
auto a = 1, static s = 3

Code Explanation

  1. auto int a = 0;: every time counter() runs, a new a is made and set to 0. After a + 1, it is always 1.
  2. static int s = 0;: this line runs only once, the first time. After that, s keeps its value.
  3. Call 1: s becomes 1. Call 2: s becomes 2. Call 3: s becomes 3.
  4. s is still local: main() cannot use it directly. Only its lifetime is the whole program.

How Storage Classes Work in Memory

  • auto variables live on the stack, which is cleaned up when a function returns.
  • register variables may be kept inside the CPU itself, which is the fastest place.
  • static and extern (global) variables live in the data segment, which exists from the start to the end of the program and is filled with zeros at the start.

Example 2: Using extern to Access a Variable Defined Later

#include <stdio.h>

extern int total;        /* declaration: "total exists somewhere" */

int main(void)
{
    printf("Total = %d\n", total);
    return 0;
}

int total = 100;         /* definition: memory is created here */

Output

Total = 100

Without the extern line, main() would not know about total, because it is defined below main(). In bigger programs, the definition is usually in another .c file.

Important Points to Remember

  • 4 storage classes: auto, register, static, extern.
  • auto is the default for local variables.
  • static local variables keep their value between calls.
  • static and extern default to 0; auto and register default to garbage.
  • extern declares a variable without creating it.

Common Mistakes Beginners Make

MistakeWhat happensFix
Thinking static makes a variable globalA static local is still only usable in its functionStatic changes lifetime, not scope
&i on a register variableCompile errorDon't use register if you need the address
extern int x; with no definition anywhereLinker error: undefined reference to 'x'Define it once: int x;
extern int x = 5; inside a functionErrorInitialize at the definition, not at an extern declaration

Storage Classes Practice Questions

  1. What is a storage class? Name the four storage classes.
  2. Write the storage, default value, scope and lifetime of a static variable.
  3. What is the difference between auto and static?
  4. Why can't we use & with a register variable?
  5. What does extern do?
Show answers
  1. It defines the storage location, default value, scope and lifetime of a variable. auto, register, static, extern.
  2. Memory (data segment), default 0, local scope (or file scope if global), lifetime is the whole program.
  3. An auto variable is created and destroyed on every call and starts with garbage; a static variable is created once, keeps its value between calls and starts with 0.
  4. Because it may be stored in a CPU register, which has no memory address.
  5. It declares a variable that is defined elsewhere, so it can be used without creating a new one.

Coding Exercise

Write a function next_token() that returns a new token number every time it is called (101, 102, 103, ...) using a static variable. Call it 3 times from main().

Show solution
#include <stdio.h>

int next_token(void)
{
    static int token = 100;
    token = token + 1;
    return token;
}

int main(void)
{
    printf("Token: %d\n", next_token());
    printf("Token: %d\n", next_token());
    printf("Token: %d\n", next_token());
    return 0;
}
Token: 101
Token: 102
Token: 103

Quick Quiz: Storage Classes in C (MCQ)

  1. Which is the default storage class of a local variable?

    A) static   B) extern   C) auto   D) register

  2. A static local variable keeps its value:

    A) Only once   B) Between function calls   C) Never   D) Only in main()

  3. The default value of a static variable is:

    A) Garbage   B) 1   C) 0   D) Undefined

  4. Which storage class may store a variable in a CPU register?

    A) auto   B) register   C) static   D) extern

  5. extern int x; is a:

    A) Definition   B) Declaration   C) Function   D) Error

Show answers
  1. C: auto.
  2. B: between function calls.
  3. C: 0.
  4. B: register.
  5. B: a declaration (no memory is created).

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

4 storage classesauto, register, static, extern
autoStack, garbage, local, till block ends
registerCPU register, garbage, local, till block ends
staticData segment, 0, local, whole program
externData segment, 0, global, whole program
Default for localsauto
Keeps value between callsstatic

Frequently asked questions

What is the use of a static variable?
It keeps its value between function calls, because it lives for the whole program.
Does static change the scope of a local variable?
No. It changes only the lifetime; the scope stays local.
What is the difference between extern declaration and definition?
extern int x; only declares it; int x; defines it and creates memory.
Is register always stored in a register?
No. It is only a request; the compiler may ignore it.

Remember

  • Order: A-R-S-E.
  • A, R = short life, garbage. S, E = whole program, 0.

Next Topic

Next lesson: printf() Function in C. Module 03 starts with the most-used function in C: printf().