Compartir a través de


C Storage Classes

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at C 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

__declspec ( extended-decl-modifier-seq ) /* Microsoft Specific */

Except for __declspec, you can use only one storage-class-specifier 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 and __declspec are semantically different from the other four storage-class-specifier terminals, they are discussed separately. For specific information on typedef, see Typedef Declarations. For specific information on __declspec, see Extended Storage-Class Attributes.

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 terminals in each kind of declaration and explain the default behavior when the storage-class-specifier is omitted from a variable. Storage-Class Specifiers with Function Declarations discusses storage-class specifiers used with functions.

See Also

Declarations and Types