1.register: Request the compiler to store variables as much as possible in the CPU's registers.
The Register keyword can save the time that the CPU presents data from memory and improve the execution efficiency of the program.
But since the registers in the CPU are limited, it is impossible to put all the variables in the CPU, so we modify the variables that are frequently accessed by register.
Precautions:
(1) The register can only modify local variables and cannot modify global variables and functions.
(2) The Register modifier variable cannot use the & symbol to obtain the address of the variable, because the variable may be stored in the CPU register.
(3) The variable that the register modifies must be the type accepted by the CPU.
2.static:static-Modified variables are stored in the global data area, which can either modify the variables or modify the functions, and modify the variables to protect local and global variables.
(1) Modify the local variable to extend its life cycle to the end of the entire program.
(2) When modifying a global variable, this global variable is accessible only in this file and not in other files.
(3) When modifying a function, this function can only be invoked within the function in this file and cannot be invoked within the function within other files.
3.extern: External declaration, which indicates that the variable is defined in other files and needs to be found in other files.
4.const: Modifier variable makes this variable read-only variable, but the space of this variable is still variable, just can't change the corresponding value of this space by variable name.
Often used in function parameters, so that the function in the implementation process does not modify the value of the argument variable.
5.typedef: Rename the data type, reduce the number of changes in the program, and increase the portability of the code.
6.volatile:volatile reminds the compiler that the variables defined later can change at any time, so the compiled program will read the data directly from the variable address each time it needs to store or read the variable. If there is no
volatile keyword, the compiler may optimize read and save, may temporarily use the value in registers, if this variable is updated by another program, there will be inconsistencies.
Generally speaking, volatile is used in several places as follows:
(1) The variable required for other procedures to be modified in the Interrupt service program needs to be added volatile;
(2) The volatile should be added to the symbol sharing between tasks in the multitasking environment;
(3) Memory-mapped hardware registers are usually added volatile, because each read and write to it may be different meanings;
The situation often also takes into account the integrity of the data (a few of the interrelated flags have been read in half interrupted), and in 1 You can pass off the interrupt to present, 2
Can prohibit task scheduling, 3 can only rely on the good hardware design.
Volatile is always about optimization, and compilers have a technique called data flow analysis, where variables in the analysis program are assigned, where they are used, and where they are lost
, the analysis results can be used for the optimization of constant merging, constant propagation and so on, which can further kill code elimination. But sometimes these optimizations are not required by the program, which
When you can use the volatile keyword to prohibit these optimizations, the literal meaning of the volatile is variable, and it has the following effect:
(1) The volatile variable is not cached in the register between two operations. In multitasking, interrupts, and even setjmp environments, variables may be
Program changes, the compiler itself can not know, volatile is to tell the compiler this
Situation
(2) Do not do constant merging, constant propagation, and other optimizations, so like the following code:
volatile int i = 1;
if (i > 0) ...
If conditions are not treated as unconditional true.
(3) Reading and writing to volatile variables will not be optimized. If you assign a value to a variable but don't use it later, the compiler can often omit that assignment.
, however, the processing of memory mapped IO cannot be optimized.