加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C中的全局变量是静态的还是不是?

发布时间:2020-12-16 09:10:50 所属栏目:百科 来源:网络整理
导读:默认情况下,C静态或外部的全局变量是? 如果全局变量默认是静态的,那么它意味着我们可以在单个文件中访问它们,但我们也可以在不同的文件中使用全局变量. 这是否意味着默认情况下它们具有外部存储? 解决方法 如果未指定存储类(即extern或static关键字),则默
默认情况下,C静态或外部的全局变量是?
如果全局变量默认是静态的,那么它意味着我们可以在单个文件中访问它们,但我们也可以在不同的文件中使用全局变量.
这是否意味着默认情况下它们具有外部存储?

解决方法

如果未指定存储类(即extern或static关键字),则默认情况下全局变量具有外部链接.从C99标准:

§6.2.2 Linkages of identifiers

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.

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.

因此,即使您没有指定extern关键字,仍然可以通过其他源文件(所谓的转换单元)访问全局变量,因为它们仍然可以对同一变量进行extern声明.如果使用static关键字指定内部链接,那么即使在另一个源文件中存在相同变量名的extern声明,它也会引用另一个变量.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读