Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply link attributes in C language

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use C language link properties, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

What are link properties

The link attribute is related to the linking process of each target file and function in C language, which is used to determine whether the identifiers of different files (that is, various names defined in the program, including variable names and function names) are the same entity. More generally, it is whether the variables and function declarations in two different files point to the same entity. For example: an and b files declare the variable c at the same time, and the link attribute specifies whether the two variables c are the same c.

To put it simply, the role of link properties is to enable you to decide whether or not to access variables and functions in file b in a file.

Classification of link attributes

There are three types of link properties:

External-external link

Internal-Internal Link

None-No link

For identifiers of external attributes, multiple identifiers of the same name that appear in different files point to the same entity. In the C language, use the extern keyword to specify in the declaration to reference the same identifier defined in other files.

For identifiers for internal attributes, the identifier points to the same entity only within the current file. In C, use the static keyword in the declaration to make the identifier private to the file (only identifiers that are external for the default link attribute can be changed to internal with the static keyword).

For the identifier of the none attribute, it is a new entity at each declaration location. In C language, there are no corresponding keywords.

Default link properties

The default link attribute of an identifier is related to where it appears.

The global variable of the program, and the default link property of all functions is external.

The default link property for the rest of the identifiers is none.

In the following example, the link attribute of b, c, and f is external:

Typedef char * aint termitint c (int d) {int e; int f (int g);} practical application of extern

If you want to use the external attribute identifier defined in the b file in the a file, you can declare it in the a file using the extern keyword.

Even if the location where the identifier is located, the default link property is external, it is recommended that you use the extern keyword to indicate explicitly, which helps to increase the readability of the program.

Static

A global identifier is defined in file a, but you don't want to be accessed by other files. You can add the static keyword to that identifier.

The same identifiers are defined in files an and b, and multiple definitions can be avoided through the static keyword.

Reminder again: only for identifiers whose default link attribute is external, can you use the static keyword to change its link attribute to internal

Some details.

For the identifier of the external attribute, you can declare it in several different source files, but you can only initialize it in one place. Otherwise, there will be the problem of duplicate definition: multiple definition of'asides;.

The identifiers declared by the extern keyword are used to access identifiers of the same name defined in other files, so they cannot be initialized. If you initialize a variable declared by extern, it generates a warning: warning:'a 'initialized and declared' extern'.

If the corresponding identifier definition does not exist in other files, but the extern declaration is used in the current file, an error will be reported: undefined reference to'a declaration, the principle is the same as 2.

The identifier of the external attribute is always a static storage type.

The static keyword also has the function of changing the storage type, so it is context-dependent, and only for identifiers whose default link attribute is external can change the link property.

In C++, the const variable implicitly has an internal attribute, but not in C #.

Thinking questions

What is the difference between the internal and none attributes except for scope differences?

The following code illustrates some of the differences to some extent:

Static int i; / / definition / / static storage / / internal linkagevoid f (void) {extern int i; / / declaration / / refers to the static i at file scope / / note that even though the specifier is extern / / its linkage is intern (this is legal in both Compact +) {int I / / definition / / automatic storage / / no linkage}}

In fact, link attributes and scope are two concepts. The above problem arises because the location of the internal attribute identifier is in the file scope, while the none is often in the code block scope. To make readers think more deeply about internal links here, see question 2.

The file scope already allows the program to access the same file variable, so what's the point of internal linking?

Only one identifier can exist in the link, so the external link of the same name can be isolated by the internal link, and the external compilation unit cannot access the global identifier of the file.

A.c:

# include static int axi3witint main (void) {extern int a; printf ("averse% d\ n", a); return 0;}

B.c:

Int axi1

The compiled result is:

Axi3

The above is only an imperfect personal conjecture, leaving aside the isolation of external links, as far as the name of internal links is concerned, the author still has doubts about why the design of links should be used within the same file. but at present, the answer to this topic is beyond the author's understanding, so a more in-depth discussion is left blank for the time being.

Expansion: interested students can try to learn the relevant knowledge of links, and may find a more definite answer.

The above is all the contents of this article entitled "how to apply Link attributes in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report