Summary on Storage Classes in C

We have studied, in previous lessons, the storage classes for both variables and functions.

Now, with this information in our possession, we can see a summary diagram of storage classes in C language.

Summary Diagram of Storage Classes

Let's analyze the following program. It shows the possible combinations of storage classes for variables and functions in C language.

int a;
extern int b;
static int c;

void f(int d, register int e) {
    auto int g;
    int h;
    static int i;
    extern int j;
    register int k;
}

extern void l(int m);
static void n(int o);

The properties of the variables and functions above are shown in the following table:

Type Variable or Function Linkage Visibility Lifetime
Variable a External File Static
Variable b N/A File Static
Variable c Internal File Static
Function f External File N/A
Variable d Internal Block Automatic
Variable e Internal Block Automatic
Variable g Internal Block Automatic
Variable h Internal Block Automatic
Variable i Internal Block Static
Variable j N/A Block Static
Variable k Internal Block Automatic
Function l External File N/A
Function n Internal File N/A
Table 1: Properties of variables and functions in the example.

A note regards variables b and j. These variables are declared as extern, so since they are not defined in this source file, it is not possible to determine their linkage. Therefore, in the table, we have indicated the linkage as N/A, that is Not Applicable.

Observing the table, we can notice that the most important storage classes are static and extern. In fact, auto is redundant and has no effect while with modern compilers the register class has assumed an increasingly minor importance.

In summary:

Definition

Storage Classes in C Language

Variables in C language can have the following storage classes:

  • extern: external linkage, file visibility, static lifetime.
  • static: internal linkage, file visibility, static lifetime.
  • auto: internal linkage, block visibility, automatic lifetime (default and redundant).
  • register: internal linkage, block visibility, automatic lifetime. Cannot take the address of a variable.

Functions in C language can have the following storage classes:

  • extern: external linkage, file visibility (default and redundant).
  • static: internal linkage, file visibility.

In Summary

In this lesson we have seen a summary of storage classes in C language. This information is fundamental to understanding how variables and functions are allocated in memory and how they are accessible within a program.

There is an additional storage class for variables: thread_local. We will study it later.

In the next lesson, instead, we will begin studying Type Qualifiers.