Scope and storage category of local variables and global variable variables in C language (auto,static,extern,register)
1. Local variables and global variables
When discussing the shape parametric of a function, it was mentioned that the parametric only allocates memory units during the call, and the call ends immediately. This indicates that the shape parametric is valid only within the function, and can no longer be used when the function is left. This variable is scoped to the scope of the variable's validity. Not only for formal parameter variables, all the quantities in C language have their own scopes. Variables are described in different ways, and their scopes are different. The variables in C language can be divided into two types, namely local variables and global variables, by scope range.
1.1 Local variables
Local variables are also known as internal variables. A local variable is defined within a function as a description. Its scope is limited to functions, and the variable is automatically reclaimed by the system after leaving the function.
"Example 1.1"
//A,b,c only valid within the F1 function intF1 (intA/*function F1*/ { intB,c; } //x, y, z only works within the F2 function . intF2 (intX/*function F2*/ { inty,z; } //M,n is valid only within the main function and is not valid within other sub-functions intMainvoid) { intM,n; }
It can be seen from the above example that three variables are defined within the function F1, a is a formal parameter, and b,c is a general variable. In the range of F1, A,b,c is valid, or the scope of a,b,c variable is limited to F1. In the same vein, X, Y, Z are scoped to F2. The scope of the m,n is limited to the main function.
The scope of the local variable also describes the following points:
1) variables defined in the main function can only be used in the main function, and cannot be used in other functions. Also, variables defined in other functions cannot be used in the main function. Because the main function is also a function, it is parallel to other functions. This is different from other languages and should be taken into consideration.
2) parameter variables are local variables that belong to the tuned function, and argument variables are local variables that belong to the keynote function.
3) allows the use of the same variable names in different functions, which represent different objects, allocate different units, do not interfere with each other, and do not confuse. As in the preceding example, the variable name of the formal parameter and the argument is N, which is fully permissible.
4) A variable can also be defined in a compound statement whose scope is only within the compound statement.
"Example 1.2"
int Main (void) { int s,a; { int b; s=a+b; // b scopes are only in compound statements, that is, inside {} }//s,a scope in main () and compound statement, two {} are valid }
"Example 1.3"
intMainvoid){ intI=2, j=3, K; K=i+j;//k=5 { intk=8; printf ("%d\n", k);//Output K=8} printf ("%d\n", k);//Output k=5}
This procedure defines the I,J,K three variables in main, where K is not assigned an initial value. In the compound statement, a variable k is defined and an initial value of 8 is assigned. It should be noted that these two k are not the same variable. The k that is defined by main is in effect outside the compound statement, whereas in a compound statement the k that is defined within the compound statement functions. Therefore, the 4th line of the program K is defined by main, and its value should be 5. The 7th line output k value, the row in the compound statement, the compound statement defined within the K function, its initial value is 8, so the output is 8, the 9th line output i,k value. I is valid throughout the program, the 7th row assigns a value of 3 to I, so the output is also 3. The 9th line is already in addition to the compound statement, the output of k should be the K defined by main, this K value from line 4th has been obtained as 5, so the output is also 5.
1.2 Global variables
A global variable is also known as an external variable, which is a variable defined outside the function. It does not belong to a function, it belongs to a source program file. Its scope is the entire source program. The use of global variables in functions should generally be described as global variables. Only global variables that are described within a function can be used. The descriptor for the global variable is extern. But a global variable defined before a function is used within that function and can no longer be described.
"Example 1.4"
intb;/*Global Variables*/ voidF1 ()/*function F1*/{...//cannot use X, y unless preceded by extern int x, y; } floatx, y;/*Global Variables*/ intFZ ()/*function FZ*/{...} Int Main (void)/*Main function*/{...}
As you can see from the above example, a, B, X, y are all global variables defined outside the function. But x, y is defined after the function F1, and there is no description of x, y within F1, so they are not valid within F1. A, B is defined at the top of the source, so it can be used without instructions in F1,F2 and main.
The "example 1.5" global variable has the same name as a local variable.
intA=3, b=5;/*A, B is a global variable*/intMaxintAintb/*A, B is a local variable*/{ intC; C=a>b?a:b; return(c);} intMainvoid) { intA=8;//The global variable A is masked within the main functionprintf"%d\n", Max (A, b));//output 8, at this time form parameter a=8}
If the external variable has the same name as the global variable in the same source file, the global variable is "masked" in the scope of the local variable, that is, it does not work.
2. Storage class for variables
2.1 Dynamic storage versus static dynamic storage
as described earlier, from the scope of the variable (that is, from the space) angle , can be divided into global variables and local variables.
from another angle, from the existence of the variable value of the lifetime (that is, from the time) point of view, can be divided into static storage mode and dynamic storage.
Static storage: means how to allocate a fixed amount of storage space during a program run.
Dynamic storage: A way to dynamically allocate storage space as needed while the program is running.
User storage space can be divided into three parts:
1) program area;
2) static storage;
3) dynamic storage;
The global variables are all stored in the static storage area, and when the program starts execution, the global variable is allocated the store, and the program line is released. They occupy a fixed unit of storage during program execution and do not allocate and release dynamically;
Dynamic Storage holds the following data:
1) function form parameters;
2) Automatic variable (local variable without static declaration);
3) The field protection and return address when the function is called;
allocates dynamic storage space at the end of the function when it is called, and when the functions are invoked.
in the C language, each variable and function has two properties: the data type and the storage category of the data. Local variables in the
2.2auto variable
function, such as those that are not specifically declared as static storage classes, are dynamically allocated storage space, and the data is stored in dynamic storage. The parameters in the function and the variables defined in the function, including the variables defined in the compound statement, belong to this class, which is allocated by the system when the function is called, and the storage space is freed automatically at the end of the function call. This type of local variable is called an automatic variable. Automatic variables are declared with the keyword auto as the storage class.
Example 1.7
int f (int a)/ * define F function, A is parameter */{ int b,c=3 ; /* define B,C as an automatic variable */ }
A is a formal parameter, B,c is an automatic variable, the initial value of C is 3. After the F function is executed, the storage unit occupied by A,b,c is automatically released.
Keyword auto can be omitted, auto does not write is implicitly designated as "Automatic Storage category", which belongs to dynamic storage.
2.3 Declaring a local variable with static
Sometimes you want the value of a local variable in a function to remain the original value after the function call ends, and you should specify the local variable as a static local variable and declare it with the keyword static.
Example 1.8 examines the value of a static local variable.
Finta) {Auto B=0; StaticC=3; b=b+1; C=c+1; return(a+b+c);}intMainvoid){ intA=2, I; for(i=0;i<3; i++) printf ("%d", F (a));
Description of the static local variable:
1) Static local variables are static storage classes that allocate storage units within a static storage area. is not released during the entire run of the program. The automatic variable (i.e. dynamic local variable) belongs to the dynamic storage class, which is the dynamic storage space, which is released after the function call is finished.
2) Static local variables are assigned the initial value at compile time, that is, only the initial value is assigned, and the initial value of the automatic variable is performed at the function call, and each call to the function re-gives the initial value, which is equivalent to executing an assignment statement.
3) If you do not assign an initial value when defining a local variable, a static local variable is automatically assigned the initial value of 0 (for a numeric variable) or a null character (for a character variable). In the case of automatic variables, if the initial value is not assigned it is an indeterminate value.
Example 1.9 prints the factorial value from 1 to 5.
int FAC (int N) { static int f=1 ; F =f*n; return (f);} int Main (void int I; for (I=1 ; i<=5 ; I++) printf ( " %d!=%d\n " ,I,FAC ( i));}
2.4register variables
To improve efficiency, the C language allows local variable values to be placed in registers in the CPU, which are called "register variables" and are declared with the keyword register.
"Example 2.0" uses a register variable.
intFacintN) {Registerinti,f=1; for(i=1; i<=n;i++) F=f*I; return(f);}intMainvoid){ inti; for(i=0; i<=5; i++) printf ("%d!=%d\n", I,FAC (i));}
Description:
1) only local automatic variables and formal parameters can be used as register variables;
2) A computer system has a limited number of registers and cannot define any number of register variables;
3) A local static variable cannot be defined as a register variable.
2.5 to declare an external variable with extern
an external variable (that is, a global variable) is defined outside the function, scoped to the end of the program file, starting at the variable definition. If an external variable is not defined at the beginning of the file, its valid scope is limited to the definition to the end of the file. If the function before the definition point wants to refer to the external variable, then the variable should be declared as an "external variable" with the keyword extern before the reference. Indicates that the variable is an external variable that has already been defined. With this declaration, it is possible to use the external variable legitimately from the point of declaration.
Example 2.1 declares an external variable with extern, extending the scope in the program file.
int Max (int x, int y) { int Z; Z =x>y? x:y; return (z);} int Main (void ) { extern A B printf ( " %d\n " ,max (A, b));} int A=13 , B=-8 ;
Note: in the last 1 lines of the program file, the external variable, a, a, is defined, but since the position of the external variable is defined by the function main, the external variable, a, B, cannot be referenced in the main function. Now we use extern for A and B "external variable declarations" in the main function, which can be legitimately used from "declarations" to the external variables a and B.
[Go] delve into the scope and storage categories of local and global variables in C language