C language variable type

Source: Internet
Author: User
C language variable type

Auto
Static
External
Static external
Register

Auto local variable

The auto variable uses the stack mode to occupy the storage space. Therefore, when this segment is executed, the system immediately allocates the storage space for this variable. After the program is executed, the stack is immediately withdrawn by the system. declare in braces.
An automatic variable is a variable defined within a function. It is only allowed to be used within the defined function. Variables that cannot be used anywhere outside the function. An automatic variable is a local variable, that is, its culture is valid within the defined function. Of course, this means that automatic variables are not linked because they do not allow other files to access them. Automatic variables are invisible anywhere outside the function, so we can define variables with the same name in other places outside the function or in other functions, there will be no conflict between them. Because they all have their own culture, and they are not linked (I .e., other files are not allowed to access them ). Let's see the continuity of automatic traffic. When the computer executes this function, it creates and allocates memory for it. After the function is executed and returned, the auto-dynamic variable is destroyed. This process is implemented through a stack mechanism. When the memory is allocated for automatic variables, the stack is pressed, and the function returns the stack.

Static variables

Static variables are the variables stored by the C program compiler at a fixed address. As long as the program does not end, the memory will not be released.
The essential difference between static variables and automatic variables is that static variables do not use the stack mechanism to use memory as automatic variables do. Instead, it allocates a fixed memory for static variables, which are maintained throughout the entire process of running the program without being destroyed. This means that the persistence of static variables is the entire program running cycle. This helps us to share some data. If a static variable is defined within the function, its scope is within the function. It is valid only when used within the function, but it is different from the automatic variable, when the automatic variable leaves the function, it will not be destroyed, but the static variable will not be destroyed. It will be stored throughout the entire function running cycle. Variables defined outside the function are global variables, and all files in the project can access them, but they can only be defined once throughout the project and cannot be defined repeatedly, otherwise, an error occurs. To use this variable in other documents, you must use extern to declare this variable. This declaration is called a reference declaration. This is very important. If you have not used extern to declare a global variable that has been defined in other files, you can use it, an error occurs. If you only want to use it in the file that defines it, but do not allow it in other files, use the keyword static to declare variables outside the function. In this way, this variable is invisible to other files, that is, its connectivity and internal links. One thing we have to pay attention to is: If you declare a variable outside the function, const int A; the connectivity of variable A is an internal link and can only be used in the file that defines it. In addition, if you do not initialize the variable when defining the static variable, the static variable will be initialized to 0 by the self-S;

External variable

External variables are defined outside the program. All functions can be used in program segments.

External variables may be redefined in a program segment, and the internal variables are referenced values.

Static external variable

The difference between static external variables and external variables is that the life of external variables 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

Registers are used to allocate space by registers. The access speed is faster than the access memory and the execution speed is faster. The register size is limited.

In C language, register variables can be used to optimize program performance. The most common is to declare a common variable as a register variable: Register int Ra in a function body; if possible, the compiler will allocate a separate register for it, and all operations on this variable during function execution are performed on this register, at this time, you do not need to frequently access the database, which naturally improves the performance. But register variables are not mandatory. That is to say, even if you use the register keyword to declare a variable as a register variable, the compiler may still use it as a common variable rather than a register variable.

Note that at present, the C compiler does not allow global register variables. That is to say, register variables can only be local variables or function-shaped parameters, and preferably int, Char, or pointer-type variables. When declaring register variables, you can determine which registers to use. On the X86 platform, "EBP, EBX, ESI, and EDI" are commonly used. It can be declared as follows: Register int local_var _ ASM _ ("EBP"); at this time, special options need to be provided during program compilation, because some registers are originally used for another purpose, for example, the EBP register is originally used for Frame-pointer purposes. It can be used to track the call relationship of a program during program debugging. In this case, we use the-fomit-frame-pointer option to specify this.

 

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.