Static can be used to modify variables or functions in c.
Let's first look at the time when it is used to modify variables. Variables in c can be divided into global data areas, stacks, and stacks. In fact, we usually call stacks instead of stacks. Don't confuse them.
Int;
Int main ()
{
Int B;
Int c * = (int *) malloc (sizeof (int ));
}
A is a global variable, B is a stack variable, and c is a stack variable.
Static modification to the global variable can be considered as limiting that the 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 can be considered to extend the life cycle of stack variables to the end of program execution. In general, the life cycle of stack variables is managed by OS. During the rollback process, the life of stack variables ends. However, after static modification, variables are no longer stored in the stack, but stored together with global variables. At the same time, it cannot be used after the function that defines it is left blank. However, if you call the function that defines it again, it can continue to be used and save 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 cannot be called by functions in other files of the same program.
File a. c
Static int I; // only used in file
Int j; // used in the project
Static void init () // only used in file
{
}
Void callme () // used in the project
{
Static int sum;
}
The above global I variables and init () functions can only be used in the. c file, and the scope of the global variable sum is only in callme. The limitations of variable j and function callme () are extended to the entire project file. Therefore, you can use the extern keyword to call in B. c below. Extern tells the compiler that the variable or function has been defined in other files.
File B .C
Extern int j; // call
Extern void callme (); // call
Int main ()
{
...
}
In addition, when C and C ++ are mixed programming, if c ++ calls a function or variable defined in the c source file, so we need to add extern to tell the compiler to name the function in c mode:
File A. cpp calls the variables I and callme () in a. c ()
Extern "C" // call the variable in the c file in the c ++ File
{
Int j;
Void callme ();
}
Int main ()
{
Callme ();
}
Two static rules:
A. if the global variable is only accessed in A single C file, you can change the variable to A static global variable to reduce the coupling between modules;
B. If the global variable is only accessed by a single function, you can change the variable to the static local variable of the function to reduce the coupling between modules;
C. When designing and using functions that access dynamic global variables, static global variables, and static local variables, you need to consider re-import;
Global variables are stored externally and statically.
(1) global variables are generally stored in external storage and defined with the reserved word extern. At this time, the scope of the variable is to form all program files of the entire program, that is, the defined external variables can be used by other program files.
You must be cautious when using such global variables. Once an error occurs, it will affect the entire program.
(2) If you want global variables to be used only in this program file, but not in other program files, you must define the storage method as static storage mode and use the reserved word static to define it. Static external variables.
For example, in the filel. c file in the preceding example, if the definition is as follows:
Static int:
The scope of variable a is reduced to filel. c In the program file, which cannot be referenced in file2.c.
It is worth noting that adding static to global variables is defined as static storage, which does not mean static storage, but dynamic storage without static. The two types of global variables (external variables) are both static storage methods and are allocated storage space during compilation, but with different scopes. Static external variables facilitate error isolation and modular programming.
(3) The default storage method of global variables is external storage.
The program in the previous section does not see the storage class definition of variables. In fact, the default storage method of variables is used. Use auto for local variables and extern for global variables. This is why we haven't seen auto and extern in the program so far.