Scope, link properties, and storage types in the C language

Source: Internet
Author: User
Tags function prototype

Scope

When a variable is declared in a part of the program, he can be accessed only in a certain area of the program, and the compiler confirms 4 different types of scopes: file scope, function scope, code block scope, and prototype scope

1. code block scope : All statements that lie between a pair of curly braces are called a block of code. Any identifier declared at the beginning of a code block has a code block scope, indicating that he can be accessed by all statements in this block of code. (In the example: F,g function, i)

2. file scope: Any identifiers declared outside of all blocks of code have a file scope, which means that they are accessible from the point where they are declared until the end of the source file where they are located. (In the example: A, B)

3. prototype scope: The prototype scope applies only to the name of the parameter declared in the function prototype. (In the example: C,h)

4. function Scope: Represents a scope in a function.

Example:

int A;

int b (int c);

int d (int e) {

int f:

int g (int h);

...

{

int f,g,i;

}

{

int i;

}

}

Link Properties
When the individual source files that make up a program are compiled separately, all the target files and the functions referenced from one or more function libraries are linked together to form an executable program if the same identifiers appear in several different source files. The link property of the identifier determines how these identifiers are handled.

There are three types of link properties:external (external), internal (internal), and none (none).

1.None: Identifiers without link attributes (none) are always treated as individual individuals.

2.internal: All declarations of identifiers within the same source file refer to the same entity, but multiple declarations in different source files belong to different entities

3.external: The identifier, no matter how many times it is declared, is located in several source files representing the same entity.

Example:

typedef char *A;

int b;

int c (int d) {

int e;

int f (int g);

}

By default: The B,c,f link property is external and the remaining identifier is none.

The keyword extern and static are used to modify the link property of an identifier in a declaration. If you add static to the external property by default, you can make his property internal.

For example, the above static int b; and the static int C (int d) becomes the source file private and cannot be accessed by other files.

Static only works on declarations with the default property of external, which is completely different from the effect of none by default.

extern OK. The identifier of the None property becomes the external property.

For example, the above extern int e can be modified to use the e identifier of the other source file

Storage type

In the program, the variable storage type determines when he is created, when it is destroyed, and how long his value remains. There are three places you can use to store variables: normal memory, runtime stack, hardware register.

Normal memory : variables declared outside any block of code are always stored in static memory. Such variables are called static variables, which are created at the start of the program and destroyed at the end.

run-time stack : The default storage type of a variable declared inside a code block is automatic, called an automatic variable. If you add static to a variable declared inside a code block, the stored type of the variable is changed to static, and the program is running

Hardware registers : variables declared with the keyword register represent register variables and are stored in the registers of the hardware. He can only declare an automatic variable.

Scope, link properties, and storage types in the C language

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.