c连锁混乱(c linkage confusion)

编程入门 行业动态 更新时间:2024-10-23 21:32:04
c连锁混乱(c linkage confusion)

我是中级C程序员。 我正在浏览C中的一个简单代码片段

int a ; // A const int b; // B static int c; //C void func(int d) // D { //..... }

变量a,b,c和d的联系是什么? 我很确定默认情况下有一个外部链接, b , c和d有内部链接。 我的理解是否正确?

这是我在这个网站的第一个问题。

I am intermediate level C programmer. I was walking through a simple code snippet in C

int a ; // A const int b; // B static int c; //C void func(int d) // D { //..... }

What are the linkage of variables a,b,c and d. I am quite sure that a by default has external linkage, b, c and d have internal linkage. Is my understanding correct?

This is my first question at this site.

最满意答案

来自第6.2.2, Linkages of identifiers节6.2.2, Linkages of identifiers ,C99的确切来源:

1 /在不同范围内或在同一范围内声明的标识符不止一次可以被称为链接的过程引用相同的对象或功能。 链接有三种:外部,内部和无。

2 /在构成整个程序的翻译单元和库的集合中,每个具有外部链接的特定标识符的声明表示相同的对象或功能。 在一个翻译单元内,每个具有内部链接的标识符声明表示相同的对象或功能。 没有链接的标识符的每个声明表示唯一的实体。

3 /如果对象或函数的文件范围标识符声明包含静态存储类说明符,则标识符具有内部链接。

4 /对于在该标识符的在先声明可见的范围内用存储类别说明符extern声明的标识符,如果在先声明指定了内部或外部链接,则稍后声明中标识符的链接与在先前声明中指定的联系。 如果前面的声明不可见,或者前面的声明没有指定链接,则标识符具有外部链接。

5 /如果函数标识符的声明没有存储类说明符,则其链接的确定与使用存储类说明符extern声明一样。 如果对象的标识符声明具有文件范围而没有存储类说明符,则其链接是外部的。

6 /以下标识符没有链接:声明为对象或函数以外的任何标识符; 一个标识符被声明为一个函数参数; 在没有存储类说明符extern的情况下声明的对象的块范围标识符。

7 /如果在翻译单位内,同一标识符同时出现内部和外部链接,则行为不确定。

现在,逐个处理变量:

a由第5部分涵盖,因为它是“具有文件范围且没有存储类说明符的对象的标识符”。 因此它有外部联系。 b也包含在第5部分(文件范围,没有存储类说明符)中。 因此外部联系。 c由第3部分涵盖,因为它具有static存储类说明符 - 它具有内部链接。 最后, d被第6部分覆盖,是一个函数参数 - 它没有链接。

From section 6.2.2, Linkages of identifiers, of C99, the definitive source:

1/ An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. There are three kinds of linkage: external, internal, and none.

2/ In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.

3/ If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

4/ For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.

5/ If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

6/ The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.

7/ If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.

Now, tackling your variables one by one:

a is covered by part 5 since it's "an identifier for an object [that] has file scope and no storage-class specifier". It therefore has external linkage. b is also covered by part 5 (file scope, no storage-class specifier). Hence external linkage. c is covered by part 3 since it has the static storage-class specifier - it has internal linkage. finally, d is covered by part 6, being a function parameter - it has no linkage.

更多推荐

本文发布于:2023-07-22 12:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219941.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:混乱   linkage   confusion

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!