The C language describes variables in two aspects: variable types and Storage types of variables. Variable types such as int (integer) and char (character type) indicate the memory space occupied by the variable. The variable storage type is used to describe the scope of the variable.
C language variable Storage types include:Automatic class,Register,Static classAndExternal class.
Local variableIt refers to the variable described inside the function (also knownAutomatic Variable), Using keywordsAuto. All non-full variables are considered local variables, so auto is never used. Local variables are automatically generated when the function is called, but are not automatically initialized. As the function call ends, the variable disappears automatically. This variable is automatically generated when the function is called the next time, and the value is assigned again, it automatically disappears when you exit.
Static variablesKeywordStaticStatement. Variable types can be dividedStatic local variablesAndStatic full-process variable.
(1)Static local variables
It differs from a local variable in that this variable always exists when the function exits, but cannot be used by other functions. When you enter the function again, the last result is saved. Others are the same as local variables.
(2)Static full-process variable
Static full-process variables are variables that are visible only in the defined source file but not in other source files. It differs from full-Process Variables in that full-process variables can be described as external variables (extern) and used by other source files, but static full-Process Variables cannot be described as external variables, that is, it can only be used by the source file.
External variablesKeywordExternStatement. To enable variables to be used by other files in addition to defining their source files, you must notify eachProgramModule file, which can be described by extern.
Register variableIt is usually used when the execution speed is more important. The idea is to tell the compiler to put the variable in a CPU register. Because data operations in registers are faster than in memory, this improves the programCode. The description of register variables is to add keywords before the variable name and type.Register. It is worth noting that the bitwise operator cannot act on register variables.
The following code analyzes the types of variables.
File1.c # Include < Stdlib. h > # Include < Stdio. h > Int Reset () ; Int Next () ; Int Last() ; Int Sum ( Int ) ; Int I = 1 ; Void main () { Auto int I, J; I = Reset () ; For ( J = 1 ; J <= 3 ; J ++ ) { Printf ( "I = % d \ TJ = % d \ n" , I, j ) ; Printf ( "Next (I) = % d \ n" , Next ()) ; Printf( "Last (I) = % d \ n" , Last ()) ; Printf ( "Sum (I + J) = % d \ n" , Sum ( I + J )) ; } }
File2.c File Static int I = 10 ; Int Next() { Return ( I + = 1 ) ; } Int Last () { Return ( I -= 1 ) ;} Int Sum ( Int I ) { Static int J = 5 ; Return ( I = J + = I ) ; }
File3.c FileExtern int I; Reset () { Return ( I ) ; }
Declare I as a full-process variable at the beginning of file1.c and initialize it to 1. In the main function, two automatic variables I and j are described. In this way, the I that appears inside the main function is the automatic variable.
At the beginning of file3.c, it indicates that I is external, indicating that it is the same variable as I defined by file1.c. Therefore, the reset value returns the current value of I 1.
The file2.c file defines variable I at the beginning and declares it as a static variable. Therefore, it is only used in the current file. However, from the definition of the sum function, the I used internally is a form parameter, and J is an internal static variable, which is different from the previous I and j.
The specific execution process is analyzed as follows:
(1) first, the program is compiled to generate an executable file. After running the executable file, the function enters from the main function body. In the initialization process, I is a global variable and the initial value is 1.
(2) after entering the main function, call the function reset () and enter the file file3.c. The file file3.c declares that I is an external variable. Therefore, the value of I is 1, the returned value is 1. After the function reset () is executed, it returns to the main function and continues to execute the program in the loop body.
(3) the loop body first calls the function next (). The program enters the file2.c file and determines that I is a static variable. Therefore, the value of I is 10 and the next () is executed () then the value of I is 11, the return value is 11, and 11 is used as the value of I. When the last () function is called, the value of I is 11. After the function is executed, the value of I changes to 10 and 10 is used as the value of I. Finally, execute the sum (I) function. At this time, note that the parameter is passed during the execution of the program, that is, the actual calculated value for the first call is sum (I + J = 2 ), in this way, the execution result after the sum (I) function is called is I = 5 + 2 = 7, that is, the execution result.
(4) and so on, respectively, 7, 10, and 14. Through the above program, we can understand that different storage types have different scopes. in programming, how to flexibly use different storage types can make the program more flexible.
Appendix: running result
I = 1 j = 1
Next (I) = 11
Last (I) = 10
Sum (I + J) = 7
I = 1 J = 2
Next (I) = 11
Last (I) = 10
Sum (I + J) = 10
I = 1 J = 3
Next (I) = 11
Last (I) = 10
Sum (I + J) = 14
parse static keywords
static can be used to modify variables or functions in C.
when modifying variables. Variables in C can be divided into global data areas, stacks, and stacks. In fact, the stack we usually call is a stack that does not contain a pair. Don't confuse it.
int A;
main ()
{< br> int B;
int C * = (int *) malloc (sizeof (INT);
}< br> A is a global variable, B is a stack variable, and C is a heap variable.
static modification of global variables, which can be considered as a restriction. This variable can only be referenced in this file. Some programs are composed of many. c files. Variables can be referenced by each other. However, after static modification, the variable can only be referenced by functions in this file.
static modification of stack variables means that the lifecycle of stack variables is extended to the end of program execution. Generally, the life cycle of stack variables is managed by OS. During the rollback process, the life of stack variables is also
. However, after static modification, the variables are not stored in the stack, but stored together with global variables. At the same time, the function cannot be used after it is defined. However, if you call the function that defines it again
, it can continue to be used, and saves the value left after the previous call.
static function modification is similar to global variable modification. It can only be called by functions in this file, but not by functions in other files of the same program.