c/language/storage duration

Specify storage duration and linkage of objects and functions:


 * - automatic duration and no linkage
 * - automatic duration and no linkage; address of this variable cannot be taken
 * - static duration and internal linkage (unless at block scope)
 * - static duration and external linkage (unless already declared internal)

Explanation
Storage-class specifiers appear in. At most one specifier may be used. The storage-class specifiers determine two independent properties of the names they declare: storage duration and linkage.

@1@ The specifier is only allowed for objects declared at block scope (except function parameter lists). It indicates automatic storage duration and no linkage, which are the defaults for these kinds of declarations. @2@ The specifier is only allowed for objects declared at block scope, including function parameter lists. It indicates automatic storage duration and no linkage (which is the default for these kinds of declarations), but additionally hints the optimizer to store the value of this variable in a CPU register if possible. Regardless of whether this optimization takes place or not, variables declared cannot be used as arguments to the, and  arrays are not convertible to pointers. @3@ The specifier specifies both static storage duration and internal linkage (unless used at block scope). It can be used with functions at file scope and with variables at both file and block scope, but not in function parameter lists. @4@ The specifier specifies static storage duration and external linkage. It can be used with function and object declarations in both file and block scope (excluding function parameter lists). If appears on a redeclaration of an identifier that was already declared with internal linkage, the linkage remains internal. Otherwise (if the prior declaration was external, no-linkage, or is not in scope), the linkage is external.

If no storage-class specifier is provided, the defaults are:
 * for all functions
 * for objects at file scope
 * for objects at block scope

For any struct or union declared with a storage-class specifier, the storage duration (but not linkage) applies to their members, recursively.

Function declarations at block scope can use or none at all. Function declarations at file scope can use or.

Function parameters cannot use any storage-class specifiers other than. Note that has special meaning in function parameters of array type.

Storage duration
Every has a property called storage duration, which limits the object. There are four kinds of storage duration in C:


 * automatic storage duration. The storage is allocated when the in which the object was declared is entered and deallocated when it is exited by any means (,, reaching the end). . If the block is entered recursively, a new allocation is performed for every recursion level. All function parameters and non- block-scope objects have this storage duration,
 * static storage duration. The storage duration is the entire execution of the program, and the value stored in the object is initialized only once, prior to . All objects declared and all objects with either internal or external linkage have this storage duration.


 * allocated storage duration. The storage is allocated and deallocated on request, using dynamic memory allocation functions.

Linkage
Linkage refers to the ability of an identifier (variable or function) to be referred to in other scopes. If a variable or function with the same identifier is declared in several scopes, but cannot be referred to from all of them, then several instances of the variable are generated. The following linkages are recognized:


 * no linkage. The identifier can be referred to only from the scope it is in. All function parameters and all non- block-scope variables (including the ones declared ) have this linkage.


 * internal linkage. The identifier can be referred to from all scopes in the current translation unit. All file-scope identifiers (both functions and variables) have this linkage.


 * external linkage. The identifier can be referred to from any other translation units in the entire program. All non- functions, all variables (unless earlier declared ), and all file-scope non- variables have this linkage.

If the same identifier appears with both internal and external linkage in the same translation unit, the behavior is undefined. This is possible when are used.

Linkage and libraries
Declarations with external linkage are commonly made available in header files so that all translation units that #include the file may refer to the same identifier that are defined elsewhere.

Any declaration with internal linkage that appears in a header file results in a separate and distinct object in each translation unit that includes that file.

Library interface:

Library implementation:

Application code:

Keywords
,, , ,