There are four Storage types in C: auto, static, extern, and register.
★Auto storage type
Default storage type. In C, if the storage type of variables is ignored, the compiler willAutomatic defaultAuto type
★Register storage type
Register variable.The register is on the CPUIt features low capacity and high speed. It is generally used to store frequently used variables in programs, or someIntermediate variableTo speed up program execution. Such variables give priority to registers.
★Static storage type
Static variables are static variables used to restrict the scope. Both global variables and local variables are stored in data segments. This keyword is mainly used in three aspects: local variables, global variables, and functions.
● Apply to local variables
For a local variable whose storage type is declared static, itsSCOPE has not changedOnly the function that defines the local variable can use it. However, itsLifecycleChanged. The declaration period of a common local variable is the execution length of the function that defines it. After the execution of the function that defines it, the local variable will be canceled. But for static local variables, its lifecycle isThe entire function execution period.
● Applications and global variables
For a global variable whose storage type is declared static, itsThe lifecycle remains unchanged., During program execution, the variable always exists, but itsReduces the impact. Because the global variables defined as static can only be used by functions in this file.
Why does the scope of a global variable become smaller when it is defined as static?
Because a project does not only contain a source file, but may contain many source files.Common global variables are visible in other source files.Its scope is the whole project. to reference this global variable in other files, you only need to declare it with extern. However,When declared as static, its scope is limited to the use of this source file. This global variable is hidden and invisible to other files.
In fact, the static keyword has many advantages. In programming, different people complete different modules. To prevent different modules from using duplicate global variables, add a static variable before the global variables of each module. Also, if the external variables in this file do not need to be referenced by other files, you can set them to static external variables to avoid misuse of other files!
● Applications and functions
If static is added before a function, the function scope is only available in this file and invisible to other files.
★Extern storage type
This storage type mainly aimsReference global variables and functions of other files.