Declarations
Properties of variables
- Every variable in a C program has three properties:
Storage duration
Automatic storage duration
: is allocated when the surrounding block is executed; storage is deallocated when the block terminates, causing the variable to lose its value (no default init value).Static storage duration
: variable can stay at the same storage location as long as the program is running, allowing it to retain its value indefinitely.
Scope
Block scope
: the variable is visible from its point of declaration to the end of the enclosing blockFile scope
: the variable is visible from its point of declaration to the end of the enclosing file.
Linkage
External linkage
: the variable may be shared by several files in a program.Internal linkage
: the variable is restricted to a single file, but may be shared by the functions in that file.No linkage
: the variable belongs to a single function and can’t be shared at all.
Storage classes
auto
storage class
- The auto storage class is legal only for variables that belong to a block. An auto variable has automatic storage duration (not surprisingly), block scope, and no linkage
- Default for variables declared inside a block.
static
storage class
extern
storage class
- The extern storage class enables several source files to share the same variable.
register
storage class
Function storage class
Example
Name | Storage duration | Scope | Linkage |
---|---|---|---|
a | static | file | external |
b | static | file | x |
c | static | file | internal |
d | automatic | block | none |
e | automatic | block | none |
g | automatic | block | none |
h | automatic | block | none |
i | static | block | none |
j | static | block | external |
k | automatic | block | x |
Declarators
-
Deciphering complex declarations:
-
Using type definitions to simplify declarations