Static variables and dynamic variables
Static variables
is a variable modified with static when it is defined, in the form of
static TYPE var_name = init_value;
Dynamic variable, in the form of
TYPE var_name = init_value;
That is, there is no static modification. The =init_value can be omitted.
Distinguish between global variables defined outside the function and local variables within the function, scope, lifecycle, and when there is no explicit initialization The initial value is different.
1 Dynamic global variables:
The scope is the entire project, that is, dynamic global variables can be used in all files that are eventually compiled into executable files.
The life cycle is from the program running to the program exit, that is, throughout the entire running time.
The default initialization value is 0 when there is no explicit initialization.
2 Static global variables:
The scope is the current file, from the definition/declaration position to the end of the file.
The life cycle is from the program running to the program exit, that is, throughout the entire running time.
The default initialization value is 0 when there is no explicit initialization.
3 Dynamic local variables:
The scope is the current function, from the definition position to the end position of {} where it is located.
The life cycle is from function call to function exit.
The default initialization value is a random value when there is no explicit initialization.
4 Static local variables:
The scope is the current function, from the definition position to the end position of {} where it is located.
The life cycle is from the program running to the program exit, that is, throughout the entire running time. When the next function is called, the static local variables will not be initialized again, but will use the value when the last function exits.
The default initialization value is 0 when there is no explicit initialization.
Recommended course: C language tutorial
The above is the detailed content of Static variables and dynamic variables. For more information, please follow other related articles on the PHP Chinese website!