3.2 Storage Classes

The “storage class” of a variable determines whether the item has a “global” or “local” lifetime. C calls these two lifetimes “static” and “automatic.” An item with a global lifetime exists and has a value throughout the execution of the program. All functions have global lifetimes.

Automatic variables, or variables with local lifetimes, are allocated new storage each time execution control passes to the block in which they are defined. When execution returns, the variables no longer have meaningful values.

C provides the following storage-class specifiers:

Syntax

storage-class-specifier :
auto
register
static
extern
typedef

At most one storage-class-specifier may be given in the declaration-specifier in a declaration. If no storage-class specification is made, declarations within a block create automatic objects.

Items declared with the auto or register specifier have local lifetimes. Items declared with the static or extern specifier have global lifetimes.

Since typedef is semantically different from the other four storage-class-specifier nonterminals, it is discussed separately in “Typedef Declarations”.

The placement of variable and function declarations within source files also affects storage class and visibility. Declarations outside all function definitions are said to appear at the “external level.” Declarations within function definitions appear at the “internal level.”

The exact meaning of each storage-class specifier depends on two factors:

Whether the declaration appears at the external or internal level

Whether the item being declared is a variable or a function

“Storage-Class Specifiers for External-Level Declarations” and “Storage-Class Specifiers for Internal-Level Declarations” describe the storage-class-specifier nonterminals in each kind of declaration and explain the default behavior when the storage-class-specifier nonterminal is omitted from a variable. “Storage-Class Specifiers with Function Declarations” discusses storage-class specifiers used with functions.