Static usage of C language/C++, modifying variables and functions. Keywords: static local variables, static global variables, static functions, internal functions

static is used to modify variables or functions in C language/C++

1. Static modified variables

Local variables or global variables modified by static are called static local variables or static
global variables , which belong to the static storage category. Memory units are allocated in the static storage area and are not released during the entire program running.
A static local variable is assigned a value at compile time, that is, it is only assigned once from beginning to end, and it already has an initial value when the program is running. In the future, the initial value will not be reassigned every time the function is called, but the value at the end of the last function call will be retained.

The role of static global variables:
1. Make its scope limited to the file where the variable is defined (that is, from the variable definition to the end of this file), and other files cannot be accessed by any means. It is very useful when the team develops and contributes code , which can avoid the same name as other people's variables.

Functions of static local variables:
1. Static local variables defined in a function body can only be accessed in this function body, even other functions in the same file cannot be accessed.
2. Static local variables are always stored in the static data area, so even if the function finishes running, the value of the static local variable will not be destroyed, and the value will still be used when the function is used next time.
insert image description here

Two, static modification function

Add static before the function, then this function becomes a static function (internal function)

The role of static functions: it is consistent with the role of static global variables, and other file access is prohibited. It is also commonly used in team development.

Guess you like

Origin blog.csdn.net/weixin_47964723/article/details/123203276