我经历了以下两个问题:
static and extern global variables in C and C++
global variable in C are static or not?
这两个问题以不同的方式说明了这两件事.
问题1的答案:
Global variables are not extern nor static by default on C and C++.
问题2的答案:
If you do not specify a storage class (that is, the extern or static keywords), then by default global variables have external linkage
我需要知道以下内容:
>默认情况下,链接(或)中的全局变量extern是否等同于通过指定extern存储类来声明变量?
>默认情况下,全局变量是否在范围(或)中是静态的,它是否等同于通过指定静态存储类来声明变量?
> C或C有什么区别吗?有人可以澄清一下吗?
解决方法:
is global variables are
extern
by default in linkage (or) it is equivalent to declaring variable by specifyingextern
storage class?
在最外层,任何块外部声明的变量的默认存储持续时间,范围和链接具有静态存储持续时间,文件范围和外部链接. C11标准说:
6.2.1标识符的范围(p4):
[…] 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):
[…] If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is
external
.
6.2.4物体的贮存期限(p3):
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 specifierstatic
, has static storage duration.
所以,如果x是全局的
int x;
那么它的存储持续时间,范围和联系相当于x in
extern int x;
is global variables are
static
by default in scope (or) it is equivalent to declaring variable by specifyingstatic
storage class?
没有.正如我上面所说,它的持续时间是静态的,它有文件范围.
If there is any c or c++ difference please clarify?
没有不同.两种语言的规则相同.