全局变量默认是extern还是等于在global中用extern声明变量? [英] Are global variables extern by default or is it equivalent to declaring variable with extern in global?

查看:580
本文介绍了全局变量默认是extern还是等于在global中用extern声明变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了以下两个问题:

I have gone through following two questions:

C和C ++中的静态和外部全局变量

C中的全局变量是否是静态的?

两个问题都以不同的方式说明这两件事。

Both questions says the two things in different way.

问题1的答案:


在C和C ++上,默认情况下全局变量既不是外部变量也不是静态变量。

Global variables are not extern nor static by default on C and C++.

问题2的答案:


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

If you do not specify a storage class (that is, the extern or static keywords), then by default global variables have external linkage

我需要了解以下内容:


  1. 全局变量 extern 在链接中默认(或)是否等效试图通过指定 extern 存储类来声明变量?

  2. 全局变量是否为 static 默认情况下,作用域(或)是否等同于通过指定 static 存储类来声明变量?

  3. C C ++ ?有人可以澄清吗?

  1. Are global variables extern by default in linkage (or) is it equivalent to declaring variables by specifying extern storage class?
  2. Are global variables static by default in scope (or) is it equivalent to declaring variables by specifying static storage class?
  3. Is there any difference with C or C++? Can anyone please clarify?


推荐答案


全局变量是 extern 在链接中默认为(或)是否等效于通过指定 extern 存储类来声明变量?

is global variables are extern by default in linkage (or) it is equivalent to declaring variable by specifying extern storage class?

在任何块外声明的变量的默认存储持续时间,范围和链接在最外层具有静态存储期限,文件范围和外部链接。 C11标准表示:

The default storage duration, scope and linkage of variables declared outside any block, at the outer most level, have static storage duration, file scope and external linkage. C11 standard says that:


[...]如果声明标识符的声明符或类型说明符出现在任何参数块或参数列表之外,标识符具有文件作用域 ,该标识符终止于翻译单元的末尾。 [...]

[...] If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at the end of the translation unit. [...]


6.2.2标识符的链接(p5):



[...]如果对象标识符的声明具有文件范围且没有存储类说明符,则其链接为外部


6.2.4对象的存储持续时间(p3):



对象的标识符声明时不带存储类说明符
_Thread_local ,并且具有 external internal 链接或具有存储类说明符 static ,具有静态存储持续时间

An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration.

因此,如果 x 是全局的

int x;

则其存储时间,范围和链接等效于 x in

then its storage duration, scope and linkage is equivalent to x in

extern int x;   



全局变量是 static 默认情况下,作用域(或)是否等效于通过指定 static 存储类来声明变量?

is global variables are static by default in scope (or) it is equivalent to declaring variable by specifying static storage class?

没有如前所述,它的持续时间是 static ,并且具有 file 范围。

No. As I stated above that its duration is static and it has file scope.


如果c或c ++有任何区别,请澄清一下?

If there is any c or c++ difference please clarify?

没有区别。两种语言的规则都相同。

No difference. Rule is same in both languages.

这篇关于全局变量默认是extern还是等于在global中用extern声明变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆