Features:
This variable allocates memory in the global data area.
B initialization: if there is no explicit initialization, it is implicitly initialized to 0 (automatic variables are random unless explicitly initialized).
C, access variables are only visible in the source file. Strictly speaking, this file should be defined from beginning to end.
Example (from C++ programming tutorial-edited by Qian Neng P 103): //file 1. Card Print Processor (abbreviation for card print processor)
//Example 1
# include & ltiostream.h & gt
void fn();
Static int n;; //Define static global variables
void main()
{
n = 20
cout & lt& ltn & lt& ltendl
fn();
}
void fn()
{
n++;
cout & lt& ltn & lt& ltendl
}
D const constants declared under file scope default to static storage type.
Static variables allocate memory in the global data area, including the static local variables mentioned later. For a complete program, the allocation of memory is as follows:
code area
Global data area
Accumulation area
Stacking area
The dynamic data generated by the new in general program is stored in the heap area, and the automatic variables inside the function are stored in the stack area. Automatic variables generally release space with the exit of the function, and static data (even static local variables within the function) are also stored in the global data area. The data in the global data area will not release space because of the exit of the function. Careful readers may find that the code in the example 1
Static int n;; //Define static global variables
Replace with:
int n; //The program that defines global variables is still running normally. Indeed, defining global variables can realize the enjoyment of variables in files, but defining static global variables also has the following benefits:
Static global variables cannot be used by other files; (it seems different from extern)
Variables with the same name can be defined in other files without conflict;
You can change the sample code above to the following code:
//Example 2
//File 1
# include & ltiostream.h & gt
void fn();
Static int n;; //Define static global variables (used in this document only)
void main()
{
n = 20
cout & lt& ltn & lt& ltendl
fn();
}
//File 2
# include & ltiostream.h & gt
External integer n; (This variable can be referenced in other files. )
void fn()
{
n++;
cout & lt& ltn & lt& ltendl
} Compile and run Example 2. You will link to the above code, which can be compiled separately, but there are errors when linking.
Try to put staticint n; //Define static global variables
replace
int n; //Define global variables
Compile and run the program again, and carefully understand the difference between global variables and static global variables. Definition: When the static keyword is added before the local variable, the static local variable is defined. Let's give an example of a static local variable as follows://Example 3.
# include & ltiostream.h & gt
void fn();
void main()
{
fn();
fn();
fn();
}
void fn()
{
Static int n =10;
cout & lt& ltn & lt& ltendl
n++;
}
Usually a variable is defined in the function body, and whenever the program runs to this statement, the stack memory will be allocated to this local variable. However, when the program exits the function body, the system will reclaim the stack memory and the local variables will be invalid accordingly.
But sometimes we need to save the value of a variable between calls. The usual idea is to define a global variable to implement it. But in this way, variables no longer belong to the function itself, and are no longer only controlled by the function, which brings inconvenience to the maintenance of the program.
Static local variables can just solve this problem. Static local variables are stored in the global data area, not in the stack, and each value is reserved for the next call until the next new value is assigned.
Features:
This variable allocates memory in the global data area.
B. Initialization: If there is no explicit initialization, it is implicitly initialized to 0, and subsequent function calls will not be initialized.
C, it stays in the global data area until the end of the program. However, its scope is local, and its scope ends when the function or statement block that defines it ends. Note the difference from the static member function of a class.
Definition: A function is defined as a static function by adding the static keyword before its return type.
Features:
A, static function is different from ordinary function, it can only be seen in the file that declares it, and cannot be used by other files.
Example of static function://Example 4
# include & ltiostream.h & gt
Static void fn (); //Declare a static function
void main()
{
fn();
}
Void fn()// defines a static function.
{
int n = 10;
cout & lt& ltn & lt& ltendl
} Advantages of defining static functions: Static functions cannot be used by other files;
Functions with the same name can be defined in other files without conflict;