What is the difference between static global variables and ordinary global variables? What is the difference between static local variables and ordinary local variables? Difference between static global function and normal global function?

1. Static global variables are initialized only once to prevent them from being referenced in other file units; 

2. Static local variables are initialized only once, and the next time is based on the previous result value;

3. Static functions have only one copy in memory, and ordinary functions maintain a copy in each called

 

What is the role of static variables and static functions in C language The
static keyword has two meanings, you can judge by the context 

1, indicates that the variable is a static storage variable, indicates that  the variable is
stored in the static storage area. 
2, indicates that the variable is an internal connection 
(in this case, the variable is not within any {}, just like a global variable, at this time, add static ) 
, which means that in other .cpp files, the variable is not visible (you can't use it). 

When static is added in front of a function, it 
means that the function is an internal connection, which is valid in this file and cannot be used in other files. The function 
without static is global by default. 
That is to say, in other .cpp As long as you declare this function, you can use it. 

1. What is the difference between static global variables and ordinary global variables? What is the difference between static local variables and normal local variables? What is the difference between a static function and a normal function?
    Answer: The description of the global variable (external variable) is preceded by a static variable to constitute a static global variable. Global variables themselves are static storage methods, and static global variables are of course also static storage methods. The two are no different in how they are stored. The difference between the two is that the scope of non-static global variables is the entire source program. When a source program consists of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is only valid in the source file where the variable is defined, and cannot be used in other source files of the same source program. Since the scope of static global variables is limited to one source file and can only be shared by functions in the source file, errors in other source files can be avoided.
    It can be seen from the above analysis that after changing a local variable to a static variable, its storage method is changed, that is, its lifetime is changed. Changing a global variable to a static variable changes its scope and limits its scope of use.
    A static function has a different scope than a normal function. static functions are only used in this file. Functions that are only used in the current source file should be declared as internal functions (static), and internal functions should be declared and defined in the current source file. For functions that can be used outside the current source file, it should be stated in a header file. The source file that uses these functions should include this header file
    . What is the difference between static global variables and ordinary global variables: static global variables are only initialized Once, it is prevented from being referenced in other file units;
    what is the difference between static local variables and ordinary local variables: static local variables are only initialized once, and the next time is based on the previous result value;
    what is the difference between static functions and ordinary functions: static functions are in There is only one copy in memory, ordinary functions maintain a copy on each call

2. How to refer to a defined global variable?
     Answer: extern
     can be used to refer to the header file, or the extern keyword can be used. If you use the header file to refer to a global variable declared in the header file, assuming you write the variable wrong, then compile An error will be reported during the period. If you use extern reference, it is assumed that you have made the same mistake, then no error will be reported during compilation, but an error will be reported during linking.

3. Can global variables be defined in header files that can be included by multiple .C files? Why?
    Answer: Yes, you can declare global variables with the same name in static form in different C files.
    Global variables with the same name can be declared in different C files, provided that only one C file can assign an initial value to this variable, and the connection will not go wrong.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325731230&siteId=291194637