Storage categories for C-language variables

Source: Internet
Author: User

We know that from the scope of the variable (that is, from the space) point of view, can be divided into global variables and local variables.

From another angle, it can be divided into static storage mode and dynamic storage mode from the angle of time (i.e. lifetime) of the existence of variable values .

    • Static storage: Refers to how a fixed amount of storage space is allocated during a program run.
    • Dynamic storage: A way to dynamically allocate storage space as needed while the program is running.


The user storage space can be divided into three parts:

    1. program Area;
    2. static storage area;
    3. Dynamic storage area.


The global variables are all stored in the static storage area, and when the program starts execution, the global variable is allocated the store, and the program line is released. They occupy a fixed unit of storage during program execution and do not allocate and release dynamically.

The dynamic storage area holds the following data:

    1. function form parameter;
    2. Automatic variables (local variables without static declarations);
    3. The field protection and return address when the function is called.


For these data, the dynamic storage space is allocated at the beginning of the function invocation and the space is freed at the end of the function.

In the C language, each variable and function has two properties: the data type and the storage category of the data.

Auto variable

Local variables in a function, such as those not specifically declared as static storage classes, are dynamically allocated storage space, and data is stored in dynamic storage.

The parameters in the function and the variables defined in the function, including the variables defined in the compound statement, belong to this class, which is allocated by the system when the function is called, and the storage space is freed automatically at the end of the function call. This type of local variable is called an automatic variable. Automatic variables are declared with the keyword auto as the storage class.

Keyword auto can be omitted, auto does not write is implicitly designated as "Automatic Storage category", which belongs to dynamic storage.

Declaring a local variable with static

Sometimes you want the value of a local variable in a function to remain the original value after the function call ends, and you should specify the local variable as a static local variable and declare it with the keyword static.

Description of the static local variable:

    1. Static local variables are static storage classes that allocate storage units within a static storage area. is not released during the entire run of the program. The automatic variable (i.e. dynamic local variable) belongs to the dynamic storage class, which is the dynamic storage space, which is released after the function call is finished.
    2. Static local variables are assigned the initial value at compile time, that is, the initial value is assigned only once, while the initial value of the automatic variable is performed at function call, and each call function re-gives the initial value, which is equivalent to executing an assignment statement.
    3. If you do not assign an initial value when defining a local variable, the static local variable is automatically assigned an initial value of 0 (for a numeric variable) or a null character (for a character variable) at compile time. In the case of automatic variables, if the initial value is not assigned it is an indeterminate value.
Register variable

To improve efficiency, the C language allows local variable values to be placed in registers in the CPU, which are called "register variables" and are declared with the keyword register.

A few notes on the register variable:

    • Only local automatic variables and formal parameters can be used as register variables;
    • The number of registers in a computer system is limited, and any number of register variables cannot be defined;
    • A local static variable cannot be defined as a register variable.

Declaring an external variable with extern

An external variable (that is, a global variable) is defined outside the function and is scoped to the end of the program file, starting at the definition of the variable. If an external variable is not defined at the beginning of the file, its valid scope is limited to the definition to the end of the file. if the function before the definition point wants to refer to the external variable, then the variable should be declared as an "external variable" with the keyword extern before the reference. Indicates that the variable is an external variable that has already been defined. With this declaration, it is possible to use the external variable legitimately from the point of declaration.

1 #include <stdio.h>2int  main ()3{4      extern  A; 5     printf ("%d", a); 6     return 0 ; 7 }8int9;

Storage categories for C-language variables

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.