Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Understanding Storage Classes in C: auto, extern, static, register Explained

What Is a Storage Class in C?

A storage class defines a variable’s scope, location, initialization, lifetime, and accessibility. In C, every variable is not only typed but also assigned a storage class that determines where and how it can be accessed.

There are four standard storage classes:

Auto Storage Class

Variables declared with auto (the default for local variables) exist only within the block they are defined in. They are destroyed when the block exits, and they are initialized with an indeterminate value unless explicitly set.

int add(void) {
    int a = 13;
    auto int b = 48;  // auto is optional
    return a + b;
}

Scope demonstration:

#include <stdio.h>
int main(void) {
    auto int j = 1;
    {
        auto int j = 2;
        {
            auto int j = 3;
            printf("%d ", j);  // prints 3
        }
        printf("\t%d ", j);  // prints 2
    }
    printf("%d\n", j);  // prints 1
}

Output: 3 2 1

Extern Storage Class

The extern keyword declares a variable or function that is defined in another translation unit. It enables data sharing across source files.

/* main.c */
#include <stdio.h>
extern int i;  // declaration
int main(void) {
    printf("value of the external integer is = %d\n", i);
    return 0;
}

/* original.c */
#include <stdio.h>
int i = 48;   // definition

Compile both files together: gcc main.c original.c -o program. Running ./program outputs:

value of the external integer is = 48

Static Storage Class

Static variables retain their value for the duration of the program. They can be used as local statics or file‑scope statics.

/* global static */
static int counter = 7;

void next(void) {
    /* local static */
    static int iteration = 13;
    iteration++;
    printf("iteration=%d and counter=%d\n", iteration, counter);
}

int main(void) {
    while (counter < 10) {
        next();
        counter++;
    }
    return 0;
}

Output:

iteration=14 and counter=7
iteration=15 and counter=8
iteration=16 and counter=9

Register Storage Class

Use register when a variable needs to be accessed frequently and can fit in a CPU register. The compiler may ignore the hint if registers are scarce.

#include <stdio.h>
int main(void) {
    register int weight;
    /* attempting to take its address fails */
    // int *ptr = &weight;  // compilation error
    return 0;
}

Compiling the code yields:

error: address of register variable 'weight' requested

Summary Table

Storage ClassDeclaration ContextStorage LocationDefault ValueScopeLifetime
autoinside a function/blockMemoryUnpredictableFunction/blockFunction/block
registerinside a function/blockCPU registerUnpredictableFunction/blockFunction/block
externoutside all functionsMemoryZeroWhole programProgram runtime
static (local)inside a function/blockMemoryZeroFunction/blockProgram runtime
static (global)outside all functionsMemoryZeroFileProgram runtime

Key Takeaways

C Language

  1. C# Static Keyword: Mastering Static Variables, Methods, and Classes
  2. C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
  3. Understanding C Storage Classes: Scope, Lifetime, and Performance
  4. Understanding Java Nested Static Classes: Usage, Differences, and Examples
  5. Java Inner Classes Explained: Design, Syntax, and Practical Uses
  6. Understanding C Storage Classes: Scope, Lifetime, and Usage
  7. Understanding Storage Classes in C++: Scope, Lifetime, and Usage
  8. C++ Interfaces: Mastering Abstract Classes & Pure Virtual Functions
  9. Mastering C# Classes: Blueprint for Powerful Object-Oriented Programming
  10. Understanding the 5 Fire Classes: Fuel Types & Extinguishing Techniques