I also want to learn C language-Chapter 11th: Automatic variables, static variables and register variables

Source: Internet
Author: User

I. Definition of automatic variables, static variables and register variables:
Variables can also be divided into automatic variables, static variables, and register variables based on their storage locations.

Automatic variables: local variables that have not been modified are called automatic variables (they exist in the dynamic data zone-stack). The data in this zone is dynamically generated and released as the program runs, if a function is returned, it is released. If a function is called, it is generated. Keyword: The default value is atuo (not generally written ).

Automatic variables: they are allocated when the program runs to the scope of the automatic variables, rather than the declared code! It's too late to allocate code! When the execution of the function that defines the automatic variable ends, the program will release the storage space of the automatic variable and leave it to other automatic variables. Example:

# Include <stdio. h> void add (int n) {auto int s = 0; s = s + 1; printf ("No. % d: s = % d ", n, s) ;}void main () {int I = 1; for (I; I <4; I ++) {add (I );}}

Output:
No.1: s = 1
No. 2: s = 1
No. 3: s = 1

From this example, it is not difficult to see that the initial values of automatic variables must be assigned a new value after each bucket is allocated.

Static variables: static variables exist in the same data zone as global variables. However, static variables are limited at the compiler level (the compiler restricts the access to static variables (there are two levels of memory space restrictions, one is the compiler level, this access restriction is actually a syntax check by the compiler. The second case is the operating system level. In this case, if you force access, the operating system may have a blue screen or a warning Error !)). Debugging proof:

This proves that static variables exist in the same data zone as global variables.

Let's look at this example again:

Why is the compilation error here! The display x is not defined. Everyone knows this, but why ?! Why cannot I access things in the same data zone! Let's take a look at the following code:

Here we can see. 0x88888888 and 0x12312312 are clearly in the same area. Why can't they be accessed. The reason is shown in the figure below (note: The. obj file opened is the file before the link ):

It looks better to write the information into the code in the form of Annotations:

We can clearly see from here that static variables are renamed, resulting in access failure. Pay attention to the renaming rule, which integrates the information of the function in which the variable is bound to the function. There are certain rules! However, according to the internal rules of the compiler, the rules of different compilers are different. You only need to know the principle of static variables.

Static variable life cycle: Like a global variable, it starts from the executable file being loaded to the end of the program (it was born earlier than the main function, and died later than the main function ).

Local static variable: its storage zone is still in the Data zone.

Global static variables cannot boast files and are also compiler-level control.

Static variables: Private.

Register variables: I don't think this is enough, because through debugging and practice, I found that register variables are clearly chicken ribs! Don't worry about it!

 

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.