Static keywords in C language and static keywords in C Language
In C, "static" can be used to modify both functions and variables. The following functions are provided in detail:
1. static modifier. If the project contains multiple source files and the function is declared as static, the function is limited to the current file, and the external source files are invisible.
2. modify local variables in static mode. Generally, local variables are stored in the dynamic storage area. If static modification is added to local variables, the local variables are stored in the static storage area, which is valid throughout the life cycle of the program.
3. modify global variables in static mode. Similar to 1, if the project contains multiple source files, the effect of global variables declared by static is limited to this file and invisible to the outside. Other properties are the same as common global variables.
Static keywords in C Language
The process-oriented static of C ++ is the same as that of C. The static function of C ++ has two usage methods: static in process-oriented programming and static in object-oriented programming. The former applies to common variables and functions, and does not involve classes. The latter mainly describes the role of static in classes.
I. static in process-oriented design
1. Static global variables
Add the keyword static before the global variable, which is defined as a static global variable. Here is an example of a static global variable:
// Example 1
# Include <iostream. h>
Void fn ();
Static int n; // defines the static global variable.
Void main ()
{N = 20;
Cout <n <endl;
Fn ();
}
Void fn ()
{N ++;
Cout <n <endl;
}
Static global variables have the following features:
This variable allocates memory in the global data zone;
Uninitialized static global variables will be automatically initialized to 0 by the program (the values of automatic variables are random unless they are explicitly initialized );
Static global variables are visible in the entire file declared, but invisible outside the file;
All static variables are allocated memory in the global data zone, including the static local variables to be mentioned later. For a complete program, the distribution in the memory is as follows:
Code Area
Global data Zone
Heap Area
Stack Zone
In general, dynamic data generated by new is stored in the heap zone, and automatic variables in the function are stored in the stack zone. Automatic variables usually release space as the function exits, and static data (even static local variables in the function) is stored in the global data zone. The data in the global data zone does not release space because the function exits. Careful readers may find that
Static int n; // defines the static global variable.
Change
Int n; // defines the global variable
The program runs normally.
Indeed, defining global variables can share variables in files, but defining static global variables has the following benefits:
Static global variables cannot be used by other files;
Variables with the same name can be defined in other files without conflict;
You can change the Sample Code as follows:
// Example 2 // File1
# Include <iostream. h>
Void fn ();
Static int n; // defines the static global variable.
Void main ()
{N = 20;
Cout <n <endl;
Fn ();
}
// File2
# Include <iostream. h>
Extern int n;
Void fn ()
{N ++;
Cout <n <endl;
}
Compile and run Example 2, and you will find that the above Code can be compiled separately, but an error occurs during running. Try
Static int n; // defines the static global variable.
Change
Int n; // defines the global variable
Compile and run the program again to understand the differences between global variables and static global variables.
2. Static local variables
Before a local variable, add the keyword static to define the variable as a static local variable.
Here is an example of static local variables:
... The remaining full text>
What is the role of static in C language?
In the C language, the literal meaning of static can easily lead us astray. In fact, it has three functions.
(1) first role: Hide.
When we compile multiple files at the same time, all global variables and functions without the static prefix are globally visible. I will give an example to illustrate this sentence. We need to compile two source files, a. c and main. c at the same time.
The contents of A.C are as follows:
# Include <cstdio> Add this statement
Char a = 'a'; // global variable
Void msg ()
{
Printf ("Hello \ n ");
}
You may ask: why can the global variables a and msg defined in a. c be used in main. c? As mentioned above, all global variables and functions without the static prefix have global visibility, and other source files can also be accessed. In this example, a is a global variable, msg is a function, and there is no static prefix. Therefore, it is visible to other source files main. c.
If static is added, other source files are hidden. For example, if static is added before the definitions of a and msg, main. c will not be able to see them. This feature allows you to define functions with the same name and variables in different files without worrying about name conflicts. Static can be used as the prefix of functions and variables. For a function, the role of static is limited to hiding. For a variable, static has the following two functions.
(2) The second role of static is to keep the variable content persistent. Variables stored in the static data area will be initialized at the beginning of the program, which is also the only initialization. There are two types of variables stored in the static storage area: global variables and static variables, but compared with global variables, static can control the visible range of variables. static is used to hide the variables.
(3) The third role of static is that the default Initialization is 0. In fact, global variables also have this attribute, because global variables are also stored in the static data zone. In the static data area, the default values of all bytes in the memory are 0x00. In some cases, this feature can reduce the workload of programmers.
Finally, we will summarize the three functions of static in one sentence. First, the main function of static is to hide. Secondly, because static variables are stored in the static storage area, they have persistence and default value 0.
The content of main. c is as follows:
In addition to header files, you must declare the function: void msg ();
Int main (void)
{
Extern char a; // extern variable must be declared before use
Printf ("% c", );
(Void) msg ();
Return 0;
}