The storage type modifier can modify the link of the identifier and the lifecycle of the corresponding object. The identifier has a link instead of a lifecycle. The object has a lifecycle instead of a link. The function identifier is only available.Static and externModify. function parameters are only available.Register.
1. Link linkage of the identifier)
1) External links
Multiple program files in the entire program) are the same functions or objects. Common examples are extern variables declared in vitro.
2) Internal links
Indicates that only the same function or object is in the current program file. Other program files cannot be accessed. Common static variables are declared in vitro.
3) No link
Generally, the auto, register variables, and function parameters declared in the function are not linked. Its scope is inside the function.
Ii. Object life cycle)
1) Static Lifecycle
All objects with a static life cycle are created and initialized before the program starts execution. Their lifetime covers the entire program execution process. If a static variable is defined in the function, the value of the variable will be retained after the function is called for the first time. When the function is called for the second time, the value of this variable is the value at the end of the first call.
2) Automatic Life Cycle
The life cycle of an object is determined by the braces {} defined by the object. Each time a program executes a flow into a statement block, the object of the statement block's automatic lifecycle will be created and initialized at the same time.
Iii. Storage Class Modifiers
1) auto
The auto modifier can only be used for object declaration in the function. Objects with auto modifiers in the Declaration have an automatic life cycle.
In ansi c, the object declaration in the function has an automatic life cycle by default, so auto can be omitted when declared in the function.
2) register
You can use the register modifier to declare that an object has an automatic life cycle. Therefore, register can only be used in the declaration of a function.
This keyword tells the compiler that the access to this object should be as fast as possible, and it is best to store it in the CPU register. However, the compiler does not necessarily do this.
In addition, when an object is declared as register, the address operator & is unavailable because it may be put into registers.
3) static
If the function identifier is declared as static, it has a static lifecycle. If it is defined outside the function, the object has an internal link, and other program files cannot access it. If it is defined in a function, the object has no link and cannot be accessed outside the function.
Note: Only constants can be used for static variable initialization.
4) extern
If it is declared outside the function, the object has an external link and can be used in other program files. However, it may be hidden by the variable with the same name defined in the function.
If the object is declared in a function, the link to the object depends on the object defined in the current program file with the same name outside the function. If a static object with the same name is defined outside the function, the object in the function has no link; otherwise, there is an external link.
All objects of extern have a static lifecycle.
When using extern, note that the definition cannot be repeated; otherwise, an error is reported during compilation, for example:
Program file 1:
- Extern int a = 10; // compilation warning. It is best not to initialize the extern variable.
Program file 2:
- Extern int a = 20; // repeated definition, should be changed to extern int;
In general, it is best to remove the extern modifier if Initialization is required, but do not define it repeatedly). In addition, if other program files also need to use this variable, use extern to declare the variable. This will be clearer.
5) default Modifier
In the function, it is the same as auto; outside the function, it is the same as extern.
Example:
- Int func1 (void); // func1 has an external link;
- Int a = 10; // a has an external link and has a static life cycle;
- Extern int B = 1; // B has an external link and has a static lifecycle. However, there will be warnings during compilation that the extern variable should not be initialized, and you should also be aware of repeated definitions;
- Static int c; // c with internal links and static lifecycle;
- Static int e; // e has an internal link and a static lifecycle;
- Static void func2 (int d) {// func2 has internal link; parameter d has no link and automatic lifecycle;
- Extern int a; // a is the same as a variable above.) It has an external link and has a static life cycle. Note that it is not initially 0 by default. It is just a declaration;
- Int B = 2; // B has no link and will automatically survive for the same period. And hide the declared B;
- Extern int c; // c, like the above c, maintains internal links and static lifecycles. Note that it is not initially 0 by default. It is just a declaration;
- // If the extern modifier is removed, it will be similar to B, with no link and automatic life cycle, and the c declared above will be hidden;
- Static int e; // e has a connectionless and static life cycle. And hide the e declared above; the initialization value is 0;
- Static int f; // f has a connectionless and static life cycle;
- }