是默认的extern全局变量或它相当于用在全局的extern声明变量? [英] Are the global variables extern by default or it is equivalent to declaring variable with extern in global?

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

问题描述

我经历了以下两个问题了,

<一个href=\"http://stackoverflow.com/questions/11055802/static-and-extern-global-variables-in-c-and-c\">static在C和C的extern全局变量++

在C全局变量是静态的或不?

这两个问题说,在不同的方式两件事情。

问题1的答案:全局变量不是extern关键字,也不是一成不变默认情况下,C和C ++

问题2的答案:如果不指定存储类(也就是extern或static关键字),则默认情况下全局变量的外部链接

我需要知道以下几点。


  1. 是全局变量默认情况下,在联动的extern(或)也就相当于通过指定外部存储方式类声明的变量?

2.is全局变量是静态的默认范围(或)它相当于通过指定静态存储类声明变量?

3,如果有任何C或C ++的区别请你澄清?


解决方案

  

是全局变量的extern 由联动默认(或)也就相当于通过指定的extern 声明变量存储类?


的任何块之外声明变量的默认存储持续时间,范围和联动,在最外层的水平,有静态存储时间,的文件的范围和外部联动。 C11标准说:

6.2.1作用域标识符(P4):


  

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


标识符

6.2.2联系(P5):


  

[...]如果一个对象标识符的声明有文件范围,无存储类说明,其联动是外部


对象的

6.2.4存储的持续时间(P3):


  

其标识符没有存储类说明符声明的对象
   _Thread_local ,并收取的外部内部的联动或者与存储类说明,拥有的静态存储持续时间的。


所以,如果 X 是全球性的。

  INT X;

那么它的存储时间,范围和联动等同于 X

 的extern INT X;


  

是全局变量静态在域内的默认(或)其等同于指定声明变量静态存储类?


没有。正如我上面说,它的持续时间静态,它具有的文件的范围。


  

如果有任何C或C ++的区别请你澄清?


没有区别。规则是在两种语言相同。

I gone through following two questions,

static and extern global variables in C and C++

global variable in C are static or not?

Both questions says the two things in different way.

Question 1's Answer: Global variables are not extern nor static by default on C and C++.

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

I need to know the following.

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

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

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

解决方案

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

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:

6.2.1 Scopes of identifiers (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 Linkages of identifiers (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 Storage durations of objects (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 specifier static, has static storage duration.

So, if x is global

int x;

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

extern int x;   

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

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

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

No difference. Rule is same in both languages.

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

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