InC LanguageStatic (static) and extern (external) keywords are often used, but improper use mayProgramThe design is troublesome. Here we will introduce it.
1. Declare or define variables
1. Local variables:
Static variables defined in the function are static local variables stored in the static/Global Area of the memory. The life cycle of the variables is the entire source program. However, the scope is the same as that of the automatic variable and can only be used in this function. After exiting the function, this variable continues to exist but is unavailable. If you do not assign a value to a static local variable, the system automatically assigns a value of 0 (the initial value of the automatic variable is displayed immediately ).
2. Global Variables
Global variables are extern by default. Static global variables are used if they are defined. If you do not assign an initial value to a global variable, the system automatically assigns 0 values.
(1) The extern global variable can be called by functions in other source files, but it must be declared again with extern in the called file, or the global variable can be directly put in the header file, include this header file in the file that needs to be called.
(2) Static global variables. The life cycle is the entire source program, which is stored in the static/Global zone, but can only be used in the source file where it is located.
2. Declare or define functions
1. Static Functions
The lifecycle of a static function is also the entire source program, but the scope is also the source file.
2. extern Functions
The default function type is extern. When you need to call an extern function in other files, you need to declare this function with extern before use, or include the header file that declares this function. When declaring an external function in the. c file, the extern keyword must be used, while the extern keyword in the header file can be dispensable, as long as it is implemented in a. c file.
During programming, declare the global variables and functions that need to be used in other files in the header file. Include this header file in use. Do not define variables and functions in the header file.