Differences between storage class modifiers such as auto, register, static, and extern ZZ

Source: Internet
Author: User

SELF: http://kimva.blogbus.com/logs/19329180.html

 

 

1. identifier Link

(Linkage)

(1) External links
Indicates that the entire program (multiple program files) is the same function or object. 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 Lifecycle

(Lifetime)
(1) Static Lifecycle
All objects with a static lifecycle are stored inBefore the program starts to run

It is created and initialized first. 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 (but do not define it repeatedly) If Initialization is required. In addition, if other program files also need to use this variable, extern can be used to declare the variable. This will be clearer.

(5) Missing
Save Modifier
Same as auto in the function;
Outside the function, the same as extern;



Linkage Lifetime
Auto Function No linkage Automatic
Out-of-Function Syntax Error Syntax Error
Register Function No linkage Automatic
Out-of-Function Syntax Error Syntax Error
Default Function No linkage Automatic
Out-of-Function External Linkage Static
Static Function No linkage Static
Out-of-Function Internal Linkage Static
Extern Function Consistent with what it declares outside the Function Static
Out-of-Function External Linkage Static

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 (the same variable), with external links and a static lifecycle. 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.