------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
1. A global static variable is defined as a global static variable by adding the keyword static before the global variable. 1. In-memory location: static storage (static storage is present during the entire program run) 2. Initialize: Uninitialized global static variable is automatically initialized by the program to 0 (the value of the automatic object is arbitrary unless he is displayed initialized) 3. Scope: A global static variable is not visible outside the declaration of his file. Start with the definition exactly to the end of the file.
The benefit of defining a global static variable is that it is not accessed by other files, modifies variables in other files that can use the same name, and does not conflict.
2. Local static variables
The local variable is defined as a local static variable by adding the keyword static before the local variable.
- In-memory location: Static Storage Area
- Initialize: Uninitialized global static variable is automatically initialized by the program to 0 (the value of the automatic object is arbitrary unless he is displayed initialized)
- Scope: Scope is still a local scope, and when the function or block of statements defining it ends, the scope ends.
Note: When static is used to modify a local variable, it changes the location where the local variable is stored and changes from the original stack to a static storage area. But the local static variable is not destroyed after it leaves the scope, but still resides in memory until the program is finished, but we can no longer access it.
When static is used to modify a global variable, it changes the scope of the global variable (which is invisible outside of declaring his file), but does not change its location or in the static store.
3. Static functions
The function is defined as a static function by adding the keyword static before the return type of the function.
The definition and declaration of a function is extern by default, but the static function is only visible in the file that declares him and cannot be used by other files.
Benefits of defining static functions: <1> Other files can define the same name function, no conflicts <2> static functions cannot be used by other files. Storage descriptor auto,register,extern,static, corresponding to two storage periods: automatic storage period and static storage period. Auto and register correspond to automatic storage periods. A variable with an automatic storage period is established when it enters the block that declares the variable, it exists when the block is active, and is revoked when it exits the block.
The keyword extern and static are used to describe variables and functions that have a static storage period. Local variables declared with static have a static storage duration (static storage duration), or a static range (static extent). Although his value remains valid between function calls, the visibility of its name is still limited to its local domain. A static local object is first initialized when the program executes to the declaration of the object.
Because of the above characteristics of the static variable, some specific functions can be implemented. 1. The statistics function declares a local variable of the function and sets it to the static type as a counter so that the function can be counted each time it is called. This is the best way to count the number of functions being called, because this variable is closely related to the function, and the function may be called in many different places, so it is difficult to count from the caller's point of view. The code is as follows:
void count (); int main () {int i; for (i = 1; I <= 3; i++) count (); return 0;} void count () {static num = 0; num++; printf ("I have been called%d", num, "times\n");}
The output is: I have been called 1 times.
A C language program can be thought of as a series of external objects that may be variables or functions. An internal variable is a function parameter and a variable defined inside a function. External variables are defined outside the function, so they can be used in many functions. Because the C language does not allow other functions to be defined in a function, the function itself can only be "external".
Because the C language code is organized in files, an external variable or function can only be defined once in a file in all source files of a source program. Other files can be accessed through an extern declaration (the source file that defines the external variable or function can also contain an extern declaration of the external variable).
Static storage can be limited to variables or functions. If you qualify external variables and functions with static, you can limit the scope of the object to the remainder of the compiled source file. By using static to qualify external objects, you can achieve the purpose of hiding external objects. Thus, static-qualified variables or functions do not conflict with the same name in other files in the same program. If you qualify an internal variable with static, the variable has memory from the beginning of the program and does not allocate and disappear with the invocation and exit of its function.
The benefits of using static functions in the C language:
- Static functions are automatically assigned to a storage area that has been in use until the application instance is exited, avoiding the pressure stack out of the calling function, which is much faster.
- The keyword "static", translated into Chinese, is "still", so the internal function is also called the static function. But the meaning of "static" here does not mean storage, but the scope of the function is limited to this file.
The advantage of using intrinsic functions is that when different people write different functions, they don't have to worry about the functions they define, or whether they have the same name as the functions in other files, because the same name doesn't matter.
The semantics of static in C language
1.static variables:
1). Local A. Static local variables are defined within a function and have a lifetime of the entire source program, but the scope is the same as the automatic variable and can only be used within the function that defines the variable. After exiting the function, it cannot be used even though the variable continues to exist. B. The system automatically assigns a 0 value to a static local variable of a basic type if it is not assigned the initial values in the description. The value of the automatic variable is indeterminate if it is not assigned the initial values. 2). Global
Global variables themselves are static storage, and static global variables are, of course, static storage methods. But their scope, the scope of the non-static global variable is the whole source program (multiple source files can be used together), while the static global variable restricts its scope, that is, only in the source file that defines the variable, it is not available in other source files of the same source program.
A 2.static function (also called an intrinsic function) can only be called by a function in this file, not by a function in another file in the same program. Unlike general non-static functions (external functions) static in C can be used to modify variables, can also be used to modify the function. First look at the time to modify the variables. Variables in C can be divided into existing global data regions, stacks and heaps. In fact, we usually say that the stack is a stack and does not contain the right, do not confuse.
int A; main () { int b; int c* = (int *) malloc (sizeof (int));}
A is a global variable, B is a stack variable, and C is a heap variable.
Static modification of global variables can be considered as limiting the only reference to this variable in this file. Some programs are composed of a lot of. c files. You can reference variables to each other, but after adding the static modifier, this variable can only be referenced by the function in this file. Static modification of the stack variable can be considered as extending the life cycle of the stack variable to the end of the execution of the program. In general, the life cycle of a stack variable is managed by the OS, and the life of the stack variable ends in the process of unwinding the stack. But after adding the static modifier, the variable is not stored in the stack, but is stored with the global variable. At the same time, it cannot be used after the function that defines it, but it can continue to be used if the function that defines it is called again, and the value left after the previous call is saved. Static modification of a function is similar to a modification of a global variable, and can only be called by a function in this file, not by a function in another file in the same program. Static declared variables have two characteristics in the C language:
1), the variable will be placed in the program's global store, so that the next call can also maintain the original assignment. This is the difference between a stack variable and a heap variable. 2), variable with static to tell the compiler, itself only within the scope of the variable is visible. This is the difference between it and the global variable.
Dark Horse programmer--c Static in the language