The storage type can be used to describe variables and small-scale functions and form parameters. Now we will focus on the Storage types of variables.
review previous block (Block) indicates the function body (the part included in curly brackets) or composite Statement (may include declaration ). In c99, select statements (if and switch), loop statements (while, do, and for), and the "content" statements they control are also considered as blocks, although there are some differences in nature.
Properties of variables:
C language Program has the following three properties.
- Storage Duration. The storage duration of a variable determines the reserved variable and the time when the memory is released. WithAutomatic Storage PeriodThe variable gets the memory unit when the block is executed, and releases the memory unit when the block is terminated. This will cause the variable to lose its value. WithStatic Storage PeriodThe variable occupies the same storage unit during the running of the program, which allows the variable to retain its value indefinitely.
-
- Scope. The scope of a variable refers to the part of the program text that can reference the variable. Variables can haveBlock Scope(Variables are visible from the declared position to the end of the block) orFile Scope(Variables are visible from the declared position to the end of the file ).
- link . The link of the variable determines the range in which different parts of the program can share the variable. With external link variables can be several (or all) in the program) file Sharing. With internal link variables can belong to only one file, however, functions in this file can share this variable. (If a variable with the same name appears in another file, the system treats it as a different variable .) no link variables are independent functions and cannot be shared at all.
The default storage period, scope, and link of a variable depend on the location of the variable declaration.
- Variables declared within a block (including the function body) have an automatic storage duration, block scope, and are not linked.
- Variables declared at the outermost layer of a program (any block outside) have static storage periods, file scopes, and external links.
for many variables The default storage term, scope, and link are compliant. When these properties cannot meet the requirements, you can specify a specific storage type ( auto, static, extern, and register ) to change the properties of a variable.
Now let's summarize the known content.CodeThis section describes all possible methods that include or omit the storage type in variable and form parameter declarations.
Int A;
Extern Int B;
Static Int C;
VoidF (IntD, registerIntE)
{
AutoIntG;
IntH;
Static IntI;
Extern IntJ;
RegisterIntK;
}
The following table describes the properties of each variable and formal parameter in the code above.
| Variable name |
Storage Duration |
Scope |
Link |
| A |
Static |
File |
External |
| B |
Static |
File |
① |
| 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 |
① |
| K |
Automatic |
Block |
None |
① The definitions of variables B and J are not displayed here, so it is impossible to determine their links. In most cases, variables are defined in another file and have external links.
Among the four Storage types, the most important thing isExternAndStatic.AutoNo effect, while modern compilers have enabledRegisterIt became less important than before.
References:
C programming language version 2
C language programming modern method Version 2