C Language (iii) storage category

Source: Internet
Author: User
Tags function prototype

This article discusses the storage categories in C, including the storage of data in memory, the storage category of variables, the storage category of functions, and the life cycle. For the computer's storage space, there are registers and memory.

One, storage area

1. Register: Stores data that participates in the operation immediately.

2. System Area: Storage System software.

3. User program code area: The program code that holds the user program.

4. Library Program code Area: The code that holds the library function.

5. Data area, divided into heap area, stack area and static storage area.

(1) Heap: Store dynamic variables, i.e., dynamic variables requested by malloc.

(2) Stacks: variables that store types of automatic storage, including local variables in functions and compound statements.

(3) Static storage: A variable that holds a static type that is explicitly declared.

II. Storage categories for variables

1. Static variables

Static variables: Static variables in the general Program design language concept, i.e., variables that are explicitly declared in a program. A static variable has a name, and at compile time, the compiler has allocated memory space to the static variable.

The storage types of static variables fall into two main categories: static (including static and extern) and automatic (including auto)

Auto: Auto storage category, default storage category

stored in the stack area, scoped in the function and compound statement that declares it. Auto has a local lifetime, and when an individual execution of an automatic variable is declared to end, an automatic variable ends the life cycle and no longer exists.

int i; int J; // default to auto storage category

Static: Statically stored class

stored in a static storage area. Static variables are divided into static local variables and static global variables.

Static local variables: With local lifetimes, the lifetime is the entire program, and after the program ends, the lifetime ends and is deleted. But the scope is only in the individual that declares it. Leaving a compound statement or function, a static variable still exists but cannot be accessed. Once again into the individual that declares the static variable, you can continue to use it, and keep the values left by shallow use. A static local variable can be initialized only once at the beginning.

Global local Variables: The lifetime is the entire program file that contains its declaration, which is accessible at any time.

{staticint  A; ...} // Scope is only within compound statements, with auto // leave the compound statement, the static local variable exists but cannot be used, the value remains last used

extern External Storage Category

Can be used as a storage type for global variables. stored in a static storage area. In the top-level declaration, extern is the default storage class, and all non-extern global variables are treated as external variables. When using external variables for other files, use the extern specifier, which allocates the same storage space when you connect. If the same name global variables in two files do not use extern, errors will occur at the time of connection because they are all declared as global external variables, all allocated storage space, resulting in conflicts.

File 1:

int A; // extern is the default storage category for global variables

File 2

extern int A; // use local variables of file 1 to occupy the same storage space

Note: Registerregister Storage type can be explicitly declared, indicating that the variable to be allocated in the computer CPU registers, for frequently accessed variables, can save time and improve the efficiency of program operation. However, the number of registers is very limited, if the register side cool too much, will be considered the auto type.

2. Dynamic variables

Dynamic variable: Refers to the space that is applied dynamically using the request space function (such as malloc), stored in the heap area. Dynamic variables are not explicitly declared and do not allocate (or allocate) space at compile time. A dynamic variable is identified by a pointer while the program is running. When you are finished, use the free space function, such as free, to release it. Dynamic functions have dynamic lifetimes, and the life cycle is explicitly implemented.

float *p;p= (float*)malloc(sizeof(float)); // life cycle Start ...  Free (p); // end of life cycle

Third, the storage category of the function

C-language functions can be defined as static and extern two storage categories.

Static: intrinsic function

differs from the static storage category of a variable. The scope of the inner function is called scoped to this file, and static functions with the same name in different source files are not confused.

extern: External function

The default storage class for the function. External functions are defined in one source file and can be used in other source files. When calling external functions defined in other source program files in a source program file, it must be described in the original file with a function prototype, plus an extern prefix.

All functions have a static lifetime, that is, the allocated space begins before the program executes and remains until the end of the program execution.

File 1:

int f (float x)// function The default storage class is the external function {...}

File 2:

extern int f (foat); // using external functions

C Language (iii) storage category

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.