Home >Common Problem >What are static variables in C language?
Static variables in C language refer to variables modified by the static keyword. There are two types of static variables, one is global static variables defined on global variables, and the other is local static variables defined on local variables.
[Recommended course: C Language Tutorial]
Static variables
Static variables are declared through the keyword static. Next, in the article, we will introduce in detail what static variables are in C language. They have certain The reference value, I hope it will be helpful in combating.
Static means "static" and "static". Its meaning in C language is actually similar to its original meaning. It means "static" or "global". It is used to modify variables and functions. It is often used to modify variables and functions. The scope of variables or functions modified by static will change. The variable is stored on the data segment and the scope and life cycle of the variable can be changed. In C language, static variables are divided into two forms, namely all static variables and local static variables.
Global static variable
Add the keyword static in front of the global variable, and the global variable is defined as a global static variable
(1) Location in memory: Static storage area (static storage area exists during the entire program running)
(2) Initialization: Uninitialized global static variables will be automatically initialized to 0
## by the program #(3) Scope: Global static variables are not visible outside the file in which they are declared. Exactly from the point of definition to the end of the file.Local static variables
Add the keyword static in front of the local variable, and the local variable is defined as a local static variable. (1) Location in memory: static storage area(2) Initialization: Uninitialized local static variables will be automatically initialized to 0 by the program (the value of the automatic object is arbitrary , unless he is explicitly initialized) (3) Scope: The scope is still a local scope. When the function or statement block that defines it ends, the scope ends.Note
When static is used to modify a global variable, it changes the scope of the global variable (it is not visible outside the file in which it is declared) ), but its storage location has not changed, it is still in the static storage area. When static is used to modify local variables, it changes the storage location of local variables from the original stack to the static storage area. However, the local static variable is not destroyed after leaving the scope, but still resides in the memory until the end of the program, but we can no longer access it.Benefits of using static functions in C language
(1) Static functions will be automatically allocated in a storage area that is always used until the program exits, avoiding calls The push and pop of functions is much faster. (2) static means that the scope of the function is limited to this file. Don’t worry about whether the function you define will have the same name as the functions in other filesSummary: The above is the entire content of this article, I hope it will be helpful to everyone.The above is the detailed content of What are static variables in C language?. For more information, please follow other related articles on the PHP Chinese website!