-----------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! ------------
In this section, we learn about variable types and scopes in C language.
1. Global variables
A global variable is a variable that exists at the beginning of a definition, to the end of the entire program, a variable defined outside the body of all functions, stored in a static store, and all the code can be accessed, defined as follows:
#include <stdio.h>int A; Define global variable int main () { a = 10;//function can access return 0;}
It is important to note that if you need to access this global variable in another file, or if you need to use it before the variable is defined, you need to use the extern keyword to declare it:
#include <stdio.h>extern int A; Declares a global variable that exists in another file void Test () { a = 12;}
2. Local variables
A local variable is a normal variable defined within {}, decorated with auto (which can generally be omitted), so called an automatic variable, the local variable is only valid after the {} is defined, the {} is automatically destroyed after the end of the {}, and is stored in the stack space (cache), which is defined as follows:
void Test () { Auto int A;//can be the same name as the global variable, after which a is a local variable int b;//Omit Auto (general usage) Register int c;//tell the compiler to register the memory Stored variable (fast, but will not do depending on the compiler) //NOTE: The register variable can only do local variables or parameters, and he cannot get the address}
3. Static variables
Local variables are destroyed after they are used within a function, and sometimes we want this variable to be destroyed throughout the program cycle, which can be accomplished by static variables:
void Test () { static int sa=0;//The statement executes only once in printf ("%d\n", SA); sa++; The contents of a static variable are inherited from the last call (the number of times a function call can be logged here)}void Test2 () { static int sa=5;//the SA and the preceding SA are not a variable}
The SA we define here is a static variable, stored in a static storage area, which he cannot destroy throughout the program cycle, but unlike global variables, this variable can only be accessed within the defined function.
4. Const modifier words
A const modifier modifies a variable (global, local, static) that cannot be modified during its life cycle. As a result, const constants must be assigned at initialization time. The compiler optimizes the storage of const constants.
5. Volatile modifier words
In the various variables described earlier, the compiler might make some optimizations based on the differences of the computer, such as variables of the const type, because he cannot be changed, can be placed in the read faster, access to the slower static storage (this is the compiler to decide whether to do this, or other practices), The volatile modifier tells the compiler that this value is likely to change and not to be optimized for me. To put it bluntly, a variable with a volatile modifier, each time directly accesses the variable address to get the value. For example:
volatile int A; Variables with volatile modifiers are int b = A that is accessed directly through the address; int C = A;
Because A is a direct address access, he may change, B and C values may be different, here, if we do not add the volatile modifier, the compiler found that the value of a is not modified, it is possible to directly bypass the address of variable A, directly invoke the previous obtained value to operate.
Volatile in the preparation of single-chip or embedded hardware development, such as the use of more.
-----------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! ------------
Dark Horse programmer--c Language Learning-scope explanation