📚 C Programming Tutorial

C Programming

Learn C programming concepts with simple explanations, examples and practical notes.

← Back to C Programming

Storage Class

What are storage classes in C?

Storage classes in C describe the scope, lifetime, visibility, and storage behavior of variables and functions. They help us decide where a variable can be accessed and how long its value is retained during program execution.

There are four storage classes in C:
1. Automatic (auto)
2. Register (register)
3. Static (static)
4. External (extern)

Overview of storage classes

Storage class Keyword Typical scope Lifetime
Automatic auto Block/local While the block executes
Register register Block/local While the block executes
Static static Depends on where declared Entire program execution
External extern File scope or shared declaration Entire program execution for the defined object

Note: Scope and lifetime are different. Scope determines where a name can be used; lifetime determines how long the object exists.

Automatic storage class (auto)

An automatic variable is created when execution enters its block and ceases to exist when execution leaves that block.

Storage Memory
Default initial value An unpredictable value, often called a garbage value.
Scope Local to the block in which the variable is defined.
Life Till the control remains within the block in which the variable is defined.

Example: C Program using auto variables

#include 

int main(void)
{
    auto int x = 10;
    int y = 20;  // Also automatic

    printf("x = %d\n", x);
    printf("y = %d\n", y);

    return 0;
}

Output:

x = 10
y = 20

Here, auto int x = 10; explicitly declares an automatic variable. The variable y is also automatic, even though auto is omitted.

When to use: Use ordinary local variables for temporary values such as counters, intermediate calculations, and input values used within a function.

Register storage class (register)

The register keyword requests that the implementation consider storing a variable in a processor register for faster access. The compiler may ignore this request.

Storage CPU registers.
Default initial value Garbage value.
Scope Local to the block in which the variable is defined.
Life Till the control remains within the block in which the variable is defined.

Example: C Program using a register variable

#include 

int main(void)
{
    register int i;

    for (i = 1; i <= 5; i++)
    {
        printf("%d ", i);
    }

    return 0;
}

Output:

1 2 3 4 5

The variable i controls the loop. The register keyword does not guarantee that i will actually be placed in a processor register.

When to use: Usually, write ordinary local variables and let the compiler optimize them. Use register mainly when studying older C code or when required by a particular programming environment.

Static storage class (static)

A static variable has a lifetime that lasts for the entire execution of the program. Its value is retained between function calls.

Storage Memory.
Default initial value Zero.
Scope Local to the block in which the variable is defined.
Life Value of the variable persists between different function calls.

Example 1: Static local variable

#include 

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

int main(void)
{
    counter();
    counter();
    counter();

    return 0;
}

Output:

1 2 3

How does it work?

  1. First call: prints 1.
  2. Second call: prints 2.
  3. Third call: prints 3.

The variable is initialized only once and retains its updated value between calls. If count were an ordinary local variable initialized to 0, each function call would start again at zero and print 1.

Example 2: Static global variable

static int total = 100;

When declared at file scope, static gives the variable internal linkage. It can be referred to by name within that source file, but not directly by name from other source files.

When to use:
- To retain a local variable's value between function calls.
- To keep a file-level variable or function private to one source file.

External storage class (extern)

The extern keyword declares an object or function whose definition is available elsewhere, often in another part of the program or another source file.

Storage Memory.
Default initial value Zero.
Scope Global.
Life As long as the program's execution doesn't come to an end.

Example using one source file

#include 

int total = 100;   // Definition

int main(void)
{
    extern int total;  // Declaration referring to the definition

    printf("Total = %d\n", total);

    return 0;
}

Output:

Total = 100

The extern declaration refers to the file-scope definition of total.

Example using two source files

File 1: main.c

#include 

extern int total;

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

File 2: data.c

int total = 500;

When both files are compiled and linked together, the output is:

Total = 500

When to use: Use extern when a global object or function defined elsewhere needs to be declared for use in another source file. In larger projects, declarations are commonly placed in header files.

Comparison of the four storage classes

Feature auto register static extern
Main purpose Temporary local data Request register storage Retain data or limit file visibility Refer to a definition elsewhere
Default initialization Indeterminate Indeterminate Zero if not explicitly initialized Depends on the object definition
Retains value between function calls? No No Yes, for a static local variable Yes, for a defined global object
Address using & Allowed Not allowed for a register-declared object Allowed Allowed for an object
Typical use Local calculations Older optimization hints Counters, persistent state, file-private globals Sharing globals across files

Which storage class should you use, and when?

  • Use ordinary local variables (auto): For temporary values, loop counters, and calculations inside a function. This is the normal choice for most local variables.
  • Consider register only when appropriate: It is mainly of historical and educational interest. Modern compilers usually optimize variable placement automatically.
  • Use static when state must persist: For example, a function call counter that should remember its previous value, or a global variable that should be private to one source file.
  • Use extern to access an existing definition: For example, when several source files need to refer to a shared global variable or function. Avoid unnecessary global variables when local variables or function parameters will work.

Important exam points

  • The four commonly taught storage classes are auto, register, static, and extern.
  • Ordinary local variables are automatic by default.
  • A static local variable retains its value between function calls.
  • An uninitialized static variable is initialized to zero.
  • register is a request, not a guarantee of processor-register storage.
  • extern usually declares an object or function defined elsewhere.
  • File-scope static limits a variable's or function's linkage to that source file.