"Go" scope, link properties, Storage type summary--reprint study, very clear, very detailed

Source: Internet
Author: User

Identifier:

First, before discussing these three things, explain in detail the identifiers in the C language.

Identifiers are user-programmed names of variables, constants, functions, statements, and so on, just like people's names.

The naming rules for identifiers are as follows:

1. Can only be composed of letters, numbers, underscores, and the first character can not be a number;

2. You cannot use the C keyword as an identifier;

3. Sensitive to case;

Second, it needs to be clear that scope and link properties are discussed in the context of identifiers, which are discussed in the context of variables within identifiers.

Scope:

The scope of an identifier is the area in the program where the identifier can be used, as determined by its declaration position .

Divided into the following four categories:

1. File scope 2. code block scope 3. prototype Scope 4. function scope

1. File scope:

Any identifier (variable, constant, function name, statement, and so on) declared outside of all blocks of code has, which means that the identifier is accessible from the location it declares to the end of the source file.

Note: A function name declared outside of any block of code also has a file scope because it does not belong to any code block.

2. code block scope:

All statements between {} are called code blocks, and identifiers declared within a code block are scoped to the end of the block from where they are declared, and can be accessed by statements within that interval.

Note: A block of code in a nested state, when the inner layer and the layer declare the same identifier, the outer layer of the identifier no longer works inside . You should try to avoid this in programming.

3. Prototype Scope:

Applies only to parameter names declared in a function prototype, scoped to () within a function prototype, to prevent the name from being used more than once in the same prototype.

4. Function Scope:

Used only for statement labels.

Here is an example of several scopes:

#include

int A; A has a file scope

void Test (int *b); B has a prototype scope

int main (void)

{

int c=10; C has a code block scope

a=20;

printf ("a=%d", a);

printf ("Before change:c=%d", c);

Test (&C);

printf ("After change:c=%d", c);

for (int d=0;d<5;d++)//D has a code block scope

{

int e=0; E only works within the loop

printf ("d=%d", D);

}

return 0;

}

void Test (int *f)//f has a code block scope and works in function declarations

{

*f=30;

}

Link Properties:

The scope considers the case of a single source file, and the link attribute is within the context of a multi-source file . The link properties determine how to handle the same identifiers that appear in different source files .

Divided into three types:

1.external (external) 2.internal (internal) 3.none (none)

1.external: The same entity is represented in several source files, regardless of how many times the identifier is declared;

2.internal: All declarations within the same source file belong to the same entity, and multiple declarations in non-homologous files represent different entities;

3.none: Always treated as a separate entity, that is, different entities.

Attention:

① By default, the link property of a variable with a file scope, a function name is external, and the other is none;

Keyword to modify the identifier Link property:

1.static 2.extern

1.static: Used to change the external to internal, if the modified identifier is not external, then the effect of modifying the Link property is not available;

2.extern:

① for the identifier with the file scope, the extern plus does not add;

② when extern is the first time to declare an identifier, its purpose is to represent the definition of a variable or function in another file, prompting the compiler to find its definition in other modules when encountering this variable and function;

③ entities that have extern link properties always have a static storage type.

Storage type:

The type of memory that stores the value of a variable, which determines the lifetime of the variable. The storage type of a generic variable depends on the location of the declaration.

Divided into three types:

1. Static variables: Variables declared outside of any code block , stored in static memory , created before the program is run, and always present during program execution;

2. Automatic (Automatic) variable: The variable declared inside the code block is automatically variable by default, stored on the stack , and is created when the program executes the code block and automatically destroyed when the code block executes;

The default is the reason for the automatic variable:

① allocate memory when needed, reduce the total memory demand;

The ② effectively implements recursion.

3. Register variable: Used to declare an automatic variable stored in the register, the use of high-frequency variables into the hardware register, can improve efficiency.

A two-point description of static:


1. When static is used to modify a function definition, a variable declaration other than a block of code, it modifies its link properties from external->internal, without changing the scope and storage type (meaning "local");

2. When used as a variable declaration within a block of code, the storage type used to modify the variable, from auto->static, does not change scope and link properties.

"Go" scope, link properties, Storage type summary--reprint study, very clear, very detailed

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.