C-Language Data Storage Class

Source: Internet
Author: User

In C language, variables are classified into four types: Automatic Storage, static storage, external storage, and register storage.

(1). automatically store Variables

1. Nature:

-- The locality of the scope. Its scope is within the module or function where the variable definition is located (the Part enclosed by a pair of curly braces.

-- The temporary lifetime. The lifetime is the execution cycle of the function or module that the variable defines. That is, once you enter the function, C automatically creates a storage zone for the variable. Once you exit the function, C automatically revokes the storage zone.

-- The value of uninitialized variables is uncertain and meaningless.

(2) define the keywords auto (or none), auto int I, J;

Example:

Main ()
{
Int I = 100, K = 80;
Printf ("I = % d \ n", I );
{
Int I = 200;
Printf ("I = % d K = % d \ n", I, K ++ );

}
Printf ("I = % d K = % d", I, K );
Getch ();
}

(2) static storage variables

1. Definition: static data type variable name = initial value;

2. Type: global and local.

-The static local storage variable can only be used in the defined module memory. Unlike the auto variable, it can keep the original value inconvenient:

Count_up ()
{
Static int number = 25;
Number + = 25;
Return (number );
}

-Static global variables,

. Nature: Scope-entireProgram. The lifetime is permanent. The uninitialized static variable value is 0;

3. Differences between static variables and automatic storage variables.

Main ()
{
Count_up ();
Count_up ();
Printf ("\ n ");
Add ();
Add ();
Getch ();
}
Count_up ()
{
Static int num0 = 0;
Num0 + = 25;
Printf ("% d \ t", num0 );
}
Add ()
{
Auto int num1 = 0;
Num1 + = 25;
Printf ("% d \ t", num1 );
}

Running result:

25 50

25 25

(3) external storage variables

1. Use is used to separate large programs from several units (Files) for development. It indicates that the external storage variable uses the keyword extern.

(4) Register storage variables

Keyword: register, for example, register int e;

The register storage variable value is stored in the CPU register. This variable is set mainly to improve the operation speed. They are often used in loop control. Use Si and DI registers. If there are more than two defined register variables, they are treated as the automatic storage variables.

Read the full text

Category:View comments on C Language Learning

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.