4.7 C Language Storage class, scope, life cycle, link properties

Source: Internet
Author: User
Tags volatile

C language behind the operating mechanism, learned will be more familiar with C language, you can directly see the C language of the bones.

Storage class (Storage Class):

① What is a storage class?

A storage class is a variable storage type, which is what segment the variable is stored in memory.

For example: stacks: ordinary local variables.

Data segment (. Data): Initializes a global variable that is not zero, and a static local variable.

BSS segment: A global variable initialized to zero or uninitialized. (In fact, the BSS segment is the data segment initialized to 0)

Code snippet (. Text): A piece of memory area where the program executes code.

  

② Storage Class-related keywords?

Auto Static register extern volatile restrict typedef

Detailed

Auto: function: is to modify the local variables. Storage class: Local variables are stored on the stack. Stack has four features: repeated use, dirty memory, temporary, stack overflow;

  

Static: function: First static has two functions (usually remember is not very clear),

① modifies local variables to become static local variables.

What is a static local variable? A: static local variables are ordinary local variables with static modifiers;

Where are static local variables stored? Answer: static local variables are stored in the data segment;

        Why are there static local variables? For:

What is the difference between a static local variable and a non-static local variable? A: Static local variables are stored in the/BSS segment of the data segment. Non-static local variables are allocated on the stack. Because static local variables are allocated in data segments, BSS

Allows the initial value to be assigned to the static local amount of the constructed class. If the initial value is not assigned, it is automatically assigned by the system. Numerical variable automatically assigns initial value

0, the character variable assigns a null character.

② modifies global variables to become static global variables.

    The difference between a global variable and a static global variable: The link property is different,

PS: The difference between a static local variable and a global variable:

Same point:

Static local variables are the same as global variable storage classes, and are stored in the data segment/BSS segment

Static local variables and global variables have the same life cycle, which is accompanied by the entire source program.

Different points:

Scope: A static local variable is a code block scope (as with an automatic local variable). A global variable is a file scope (as with a function).

Link properties: Static local variable no link. Global variables are external links.

    

Register: Function: Store the modified variable as much as possible in the register.

Why did ① put it in the register? A: The usual variable is stored in memory, plus register, will try to put in the register, because the register and memory compared to the CPU near,

so the visit ask variables more quickly.

Note: The data is taken out of memory and placed in the register, then the CPU is then read data from the register to process, after processing the same data through register storage

In memory, theCPU does not deal directly with memory .

What are the benefits of ②?  What's the flaw? A: The advantage is that the register near the CPU, register modified variables used in that variable is repeated high frequency use, by improving the access efficiency of this variable can be extremely

When the large lifting program is running efficiently. So register is a way to maximize the efficiency of program operation.

Defect: The Register of the CPU is very small, and the variable is allocated to the Register as much as possible. Therefore, you should use caution when defining register variables.

extern: function: mainly modified

Volatile

Restrict

typedef

Summary : The occurrence of storage classes is also due to the operating system's memory management mechanism, variables are stored in different memory.

Scope (SCOPE):

① the scope of the code block for local variables;

File scopes for ② function names and global variables

③ masking rules for variables with the same name

    

Life cycle (Lifetime):

4.7 C Language Storage class, scope, life cycle, link properties

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.