The 1.static global variable is only initialized once, preventing it from being referenced in other file units;
2.static Local variables are initialized only once, the next time based on the last result value;
The 3.static function has only one copy in memory, and the normal function maintains one copy of each call.
What is the role of static variables and static functions in C language?
The static keyword has two meanings, and you look at the context to determine
1, which indicates that the variable is a static storage variable
Indicates that the variable is stored in a static storage area.
2, indicating that the variable is an internal connection
(This is the case that the variable is not within any {}, just like a global variable, with static at this point)
, which means that in other. cpp files, the variable is not visible (you cannot use it).
When static is added to the front of the function
Indicates that the function is an internal connection, which is valid in this file and cannot be applied in other files.
Non-static functions are assumed to be global.
That is, in the other. CPP just declare this function and you can use it.
1, what is the difference between a static global variable and a normal global variable? What is the difference between a static local variable and a normal local variable? What is the difference between a static function and a normal function?
A: The description of a global variable (an external variable) is preceded by static, which constitutes a static global variable. Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the whole source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.
from the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use. The
static function differs from the normal function scope. The static function is used only in this file. Functions that are used only in the current source file should be described as intrinsic (static) and intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, it should be stated in a header file that the source file to use these functions is to contain the header file
static global variables are different from normal global variables: Static global variables are only first made once To prevent references in other file units;
What is the difference between a static local variable and a normal local variable: the static local variable is initialized only once, the next time based on the last result value;
What is the difference between a static function and a normal function: The static function has only one copy in memory, and the normal function maintains a copy in each call
2. How do I reference a global variable that has already been defined?
Answer: extern
You can refer to the header file, you can also use the extern keyword, if you use a reference header file to refer to a global variable declared in the header file, assuming that you write the error, then the compilation will be an error, if you use the extern way to reference, assuming you made the same mistake, There will be no error during compilation and an error during connection.
3, global variables can be defined in multiple. C file included in the header file? Why?
A: Yes, you can declare a global variable of the same name in a different C file in static form.
You can declare a global variable of the same name in a different C file, provided that only one of the C files has an initial value assigned to it, and the connection does not go wrong.
What is the difference between a static global variable and a normal global variable? What is the difference between a static local variable and a normal local variable? What is the difference between a static global function and a normal global function?