The difference between ordinary local variables, ordinary global variables, static local variables, static global variables, ordinary functions, and static functions

Ordinary global variables and static global variables

         Before the description of the global variable (external variable), the static global variable is formed by prefixing it with static.
        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.  
 
Ordinary local variables and static local variables
         From the above analysis, it can be seen 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.                  

Ordinary and static functions
        A static function has a different scope than a normal function.
        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.
        Functions that can be used outside the current source file should be declared in a header file that is included in the source file that uses these functions.

To sum up:
What is the difference between static global variables and normal global variables?
    Static global variables are only initialized once, preventing them from being referenced in other file units.  
What is the difference between static local variables and normal local variables?
    Static local variables are initialized only once, and the next time is based on the previous result value.  
What is the difference between a static function and a normal function?
    A static function has only one copy in memory, and a normal function maintains a copy on each call.

Guess you like

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