Local variables 1> definition: A variable defined inside a function, called a local variable. Formal parameters also belong to local variables. You must assign a value before you can use the;2> scope: The local variable is only valid inside the function that defines it, that is, the local variable is used only inside the function that defines it, and other functions cannot use it.Global Variables 1> definition: A variable defined outside all functions, called a global variable. The default initialization value of 0. 2> scope: The scope of a global variable is from the location where the variable is defined to the end of the source program, where the global variable can be shared by other functions after its defined position.the storage type of the variable
* The storage type of a variable is where the variable is stored. There are 3 places you can use to store variables: normal memory, runtime stacks, hardware registers. The storage type of a variable determines when a variable is created, when it is destroyed, and how long its value remains, which determines the life cycle of the variable.
* C language depending on the storage type of the variable, variables can be divided into: automatic variables, static variables, register variables. 1. Automatic variables
1> definition: An automatic variable is stored in the stack.
2> which are auto variables: Local variables that are modified by the keyword auto are automatic variables, but rarely use this keyword, which is basically obsolete, because all local variables are automatically variable by default.
3> life cycle: Automatic variables are created when the program executes to a code block (function) that declares an automatic variable, and the automatic variables are destroyed on their own when the Code block (function) where the automatic variable is executed is completed. If a function is called repeatedly, these automatic variables are recreated each time. 2. Static variables
1> definition: Static variables are stored in static memory, that is, not on the stack.
2> which are static variables:
- All global variables are static variables
- Static variable is also a local variable modified by the keyword static
3> life cycle: Static variables are created before the program runs, and persist throughout the program's run until the end of the program. 3. Register variables
1> definition: A variable stored in a hardware register, called a register variable. Register variables are more efficient to access than stored in-memory variables (by default, both automatic and static variables are placed in memory)
2> which variables are register variables:
- Automatic variables that are modified by the key register are register variables
- Only automatic variables can be register variables, global variables and static local variables No
- Register variables are limited to int, char, and pointer type variables
3> life cycle: Because the register variable itself is an automatic variable, the register variable in the function consumes the value stored in the register when the function is called, and the variable disappears when the function ends.
4> Use Note:
- Due to the limited number of registers in the computer, too many register variables cannot be used. If the register uses saturation, the program automatically converts the register variable to automatic variable handling
- To increase the speed of operations, it is common to define a number of frequently used automatic variables as register variables, so that the program allocates as many registers as possible, instead of memory
C language-Variable type