Extern and static can be used to modify variables and functions. What is the difference and usage.
1. Variables
By storage type, there are four types of variables in C language:
The auto keyword, which can be omitted. For example, int I; variable I is the auto variable.
The external variable keyword extern is called an external variable because it must be used externally, that is, in other files. Global variables are generally stored using external variables. If an external variable is defined before, after, or referenced in another source file, it must be declared using extern.
Static variables include static global variables and static local variables. Static global variables can only be used in the current source file. The scope of a static local variable is the function body that calls the variable, but it occupies the allocated memory from start to end throughout the entire running process of the program.
The register variable is stored in the CPU register. Since the compiler has optimized the variable, this type is basically not used.
For local variables, the global variables and local variables can have the same name in the same original file. In the scope of local variables, global variables do not work.
2. Functions
Functions are divided into external and internal functions.
External function: A function can be used by other source files to define an external function. Add the keyword "extern" before the function, which is usually omitted. The default function is an external function. ,
Internal function: A function can only be called by other functions in this file. When defining an internal function, add the keyword static before the function.
The reason why so many types need to be created is to meet different functional needs, while enhancing the readability, independence and portability of source files.