C-language storage type and keywords extern, static

Source: Internet
Author: User

1. Each variable in the C language has 3 properties: storage age, scope, link

1) Shelf life: The duration of the variable's storage determines how long the memory reserved for the variable is freed. A total of 2, automatic storage period (auto), static storage period (static), automatic storage (auto) variable when the owning block is executed to obtain the memory unit, and the block terminates when the memory unit is freed, automatically obtain or free memory, hence the name auto. A static stored variable (static) always occupies the same memory unit while the program is running, static and static.

2) Scope: The scope of the variable refers to the part of the program text that can use the variable. A total of 2, block scopes are visible from the declaration location to the end of the block, where the file scope is visible from the point of declaration to the end of the file.

3) Link: A variable's link refers to different parts of the program that can share this variable. A total of 3, external links refers to the program can be different file sharing, internal link refers to only a single file, no link refers to a single function, and can not be shared at all.

2. The default storage age, scope, and link of a variable depend on the declaration position of the variable.

1) variables declared within blocks: Automatic storage term, block scope, no link.

2) variables declared at the outermost part of the program (outside any block): Static storage age, file scope, external link.

int i;  // static storage, file scopes, external links void f (void) {    INT J;    // Auto-store, block scope, no link }

3, the special use of static:

1) When outside the block, the static description variable is an internal link, note: A variable declared outside the block has an external link, plus static, becomes an internal link, the external is not visible.

2) when inside a block, static indicates that the variable is stored for static duration, note that the variable is automatically stored within the block.

Static int i;   // static storage age, file scope, internal link void f (void) {    staticint J;     // static storage, block scope, no link }

C-language storage type and keywords extern, static

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.