In the C language, a data object (often referred to as a variable) can be described by 3 attributes: Scope , storage period, link. Each property has a different value, and the combination of these different values forms the storage model for the data object, also known as the storage class, that is, how a data object exists in the computer. In the following description, we use the variable as an example (another example is a function) to represent the data object.
First, scope
The scope property describes which areas of a data object can be accessed in (source code), and it has the following values: code block, function prototype, file.
1. File scope
A function is the basic organizational unit of a C source code file, but if a variable is defined outside of all functions, i.e. not in any function body, then the variable has a file scope, ranging from the variable definition to the end of the file. It can be accessed anywhere in the range. It is important to note that the C language stipulates that all variables must be declared before being used, and that the variable cannot be accessed anywhere before the variable is defined.
2. Function prototype scope
You should first figure out what a function prototype is. Several concepts about functions are easy to confuse: function prototypes, function definitions, function declarations. Function declarations include the first two, whereas function prototypes and function definitions differ in that there is no function body, which is called a definition, not a prototype. The more fundamental difference is that the function definition is to allocate storage space, and the function prototype does not have to.
The scope of a function prototype is from the definition of the variable to the end of the prototype declaration, but this scope does not seem to be useful ...
3. Code block scope
A code block is an area that is enclosed in a pair of curly braces, and the scope of the code block is between the curly braces.
void Show (int i) {
Int J;
if (I < 0) {
int k = 2;
i = i + K;
}
j = i * 3;
Return J;
}
In this code, K is defined in the if{} block, so K is inaccessible outside the block, such as j = i * 3 , and j = i * k; it is wrong.
It is important to note that this code is a function definition, which has the form parameter I in the definition, and its scope is also the code block, that is, the function body of show{}, not the function prototype. Maybe that's why we put the function prototype scope alone.
C99 extends the concept of a code block to the parentheses of the for () and if () statement, and variables defined within parentheses also have a code block scope.
Second, storage period
This attribute describes the "time to live" of a variable, which has two values: a static storage period and an automatic storage period. The former is always present during the execution of the program, and the latter is automatically cleared by the system after use, freeing up memory.
The storage period and scope are interrelated and cannot be combined arbitrarily. In general, variables with file scope have a static storage period, while code block scope variables have an automatic storage period.
Third, the link
The Link property describes whether a variable can be referenced by another file. A C program is often composed of multiple C source files, so variables defined in one file can be much easier to refer to in another file. But there are also some variables that are specifically for this file, and do not want to be referenced by other files, otherwise it will cause confusion. Link properties are designed to solve this problem.
This property has 3 values: internal links, external links, and empty links. Links are also closely related to scopes and cannot be combined arbitrarily. In general, variables with file scope may have internal links or external links, whereas variables with code block scopes or function prototype scopes have only empty links. Variables with external connections can be used by other files, and variables with internal links can only be used inside the file, whereas variables with empty links can only be used within a block of code or a function prototype.
Four, the combination
Three attributes are formed after the "conditional" combination to form the following five storage classes: auto, register, static with outer chain, static with inner chain, static with empty chain. Use the individual attribute combinations that describe them:
5 storage classes are divided into blocks of code and files by scope, scope is determined, and others are.
The automatic and static periods of storage can be explicitly declared with the keyword auto and statics, and the inner or outer links are decorated by static and extern, respectively. However, for variables of different scopes, there is a default condition.
As can be seen from the table above, as long as the scope is a code block of variables, its storage time default is automatic, so auto is omitted to write , but to make such a variable has a static storage period, you must explicitly use the static declaration. link type, are empty links, no longer distinguish between inside and outside. It is also possible to see that such variables are defined inside the code block, and the only difference between these variables is that there is no static modification (which means that it is not statically variable).
Another big class of variables is defined in all the functions of the file-scoped variables, their storage period can only be static, can not be modified by auto, and the link type is the default outside the chain, so extern is generally omitted to write. To make this an inner chain, you need to use the static modifier.
It is possible to summarize the effect of static: it is obvious that it has different effects on variables of different scopes, the variable of code block scope, its function is to make it static variable, and for the variable of file scope, its function is to make it an inner chain variable.
What's the use of dividing variables so thin? For example, if a variable A is defined at the beginning of the file and then a variable with the same name is defined inside the function, will the computer be confused? Who is working inside the function? At this point, storage classes come in handy: different storage class variables are stored in different areas of memory, even if they are duplicate names. Inside the function, internally defined variables will overwrite the external variables, and will function as internal variables.
Also, the initialization scheme for the two variables is different: If the file scope variable is not initialized, it is automatically assigned a default value, but the code block scoped variable does not receive this treatment.
C Language Notes Storage class