C-language variable storage type auto,static,extern,static Extern,register

Source: Internet
Author: User

C Language Variable storage type

Auto
Static
extern
static extern
Register

Auto local variable

The auto variable occupies the storage space in stack mode, so when this section is executed, the system immediately allocates the memory space for the variable, and the stack is immediately retracted after the program is executed. Declared within curly braces {}.
An automatic variable refers to a variable that is defined within a function. He just allows it to be used inside the function that defines him. A variable that cannot be used anywhere other than a function. An automatic variable is a local variable, that is, its culture is valid inside the function that defines it. Of course, this means that the automatic variable is also not linked, because it does not allow other files to access him. Since an automatic variable is not visible anywhere outside of the definition of his function, it allows us to define variables that have the same name outside of the function, or within other functions, without conflict between them. Because they all have their own culture, and it's not linked (that is, no other files are allowed to access him). To see the sustainability of the automatic volume. When the computer executes this function, it creates and allocates memory for it, and the automatic variable is destroyed when the function is completed and returned. The process is implemented through a stack mechanism. allocating memory for an automatic variable presses the stack, and the function returns when the stack is rolled back.

Static statically variable


One, local static variables
Local variables can be divided into three kinds of auto, static and register according to the storage form.
Static local variables are three points different than auto type (normal) local variables:
1. Different storage space allocation
The auto type is distributed on the stack, belongs to the dynamic storage category, occupies the dynamic storage space, the function call is released automatically, and the static is allocated in the quiescent storage area, and is not released during the whole running of the program. The scope (valid range) of the two is the same, but the lifetime is different.
2. Static local variables are initialized at the first run of the module, and are operated only once.
3. For local static variables, if the initial value is not assigned, the compile period will automatically assign an initial value of 0 or a blank character, and the initial value of the auto type is indeterminate.
Characteristics: "Memory" of static local variables and "global" of life period
The term "memory" means the value of the first call to exit when the second call comes in, when the function is called two times.

Precautions:
1. "Memory", the program is very important to run the repeatability, and the static variable "memory" to destroy this repeatability, resulting in different times to run the results may be different.
2. Global and uniqueness of the "lifetime". The storage space of the ordinary local variables is allocated on the stack, so each time the function is called, the allocated space may be different, and the static has the characteristics of global uniqueness, each call points to the same memory, which causes a very important problem----NOT reentrant!!!

Second, external static variables/functions
Static in C has a second meaning: a global variable and function that represents an inability to be accessed by other files. , but in order to limit the scope of global variables/functions, a function or variable is statically added before the function becomes a static function. But the meaning of "static" here is not the way of storage, but the scope of the function is limited to this file (so called internal function). Note that at this point, for external (global) variables, regardless of whether there is a static limit, its storage area is in the static storage area, the lifetime is global. The static at this point is only a function of the domain restriction, which limits the scope within this module (file).
The advantage of using internal functions is that different people write different functions without worrying about the functions they define, or whether they have the same name as functions in other files.


extern variable

External variables are defined outside the program, and all functions are available in the program segment.

The external variable may be redefined in one of the program segments, with the variable in paragraph as the reference value.

static extern variable

The difference between static external and external variables is that external variable life can be used for multiple files at the same time, while static external variables can only be used for files that declare this variable.

Register variable

Register variable, is allocated space by the register, access speed faster than the access memory, speed up the execution speed. Register size is limited.

In C language can use register variables to optimize the performance of the program, the most common is in a function body, a commonly used variable declared as register variable: Register int ra; If possible, the compiler will assign it a separate register, the entire function during the execution of this variable is all the operation of this register, this time without frequent visits to save, naturally improve performance.
cannot be used with global variables. The situation is that VC ignores the user-defined register, so defining a register variable is the same as not defining a register, and the compiler does the same optimization, because Ms thinks that register is a valuable resource in the system and should be provisioned by the system. And that the VC compiler to optimize the ability to be greater than the average programmer. Therefore, it is also said that the Register keyword in the PC (X86CPU) is useless, the compiler by the automatic variable processing.
Note: Register is not allowed to access.
Register Int J; int *p = &j; is wrong because the register is not addressable.

Related Article

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.