<legend id='WAQPT'><style id='WAQPT'><dir id='WAQPT'><q id='WAQPT'></q></dir></style></legend>

<small id='WAQPT'></small><noframes id='WAQPT'>

      1. <i id='WAQPT'><tr id='WAQPT'><dt id='WAQPT'><q id='WAQPT'><span id='WAQPT'><b id='WAQPT'><form id='WAQPT'><ins id='WAQPT'></ins><ul id='WAQPT'></ul><sub id='WAQPT'></sub></form><legend id='WAQPT'></legend><bdo id='WAQPT'><pre id='WAQPT'><center id='WAQPT'></center></pre></bdo></b><th id='WAQPT'></th></span></q></dt></tr></i><div id='WAQPT'><tfoot id='WAQPT'></tfoot><dl id='WAQPT'><fieldset id='WAQPT'></fieldset></dl></div>
        • <bdo id='WAQPT'></bdo><ul id='WAQPT'></ul>

        <tfoot id='WAQPT'></tfoot>

        默认情况下全局变量是 extern 还是等同于在 global 中使用 extern 声明变量?

        时间:2023-12-02

          <legend id='40QNP'><style id='40QNP'><dir id='40QNP'><q id='40QNP'></q></dir></style></legend>
          <tfoot id='40QNP'></tfoot>
          • <bdo id='40QNP'></bdo><ul id='40QNP'></ul>

                  <tbody id='40QNP'></tbody>

                • <small id='40QNP'></small><noframes id='40QNP'>

                  <i id='40QNP'><tr id='40QNP'><dt id='40QNP'><q id='40QNP'><span id='40QNP'><b id='40QNP'><form id='40QNP'><ins id='40QNP'></ins><ul id='40QNP'></ul><sub id='40QNP'></sub></form><legend id='40QNP'></legend><bdo id='40QNP'><pre id='40QNP'><center id='40QNP'></center></pre></bdo></b><th id='40QNP'></th></span></q></dt></tr></i><div id='40QNP'><tfoot id='40QNP'></tfoot><dl id='40QNP'><fieldset id='40QNP'></fieldset></dl></div>

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

                  问题描述

                  我经历了以下两个问题:

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

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

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

                  问题 1 的答案:

                  <块引用>

                  全局变量在 C 和 C++ 上默认不是 extern 或 static.

                  问题 2 的答案:

                  <块引用>

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

                  我需要知道以下几点:

                  1. 链接中是否默认全局变量extern(或)是否等同于通过指定extern存储类来声明变量?
                  2. 全局变量在范围内是否默认为static(或)是否等同于通过指定static存储类来声明变量?
                  3. CC++ 有什么区别吗?任何人都可以澄清一下吗?

                  解决方案

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

                  在任何块之外声明的变量的默认存储持续时间、作用域和链接,在最外层,有static存储持续时间、file作用域和external 联动.C11 标准说:

                  6.2.1 标识符的范围 (p4):

                  <块引用>

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

                  6.2.2 标识符的链接(p5):

                  <块引用>

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

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

                  <块引用>

                  一个对象,其标识符声明时没有存储类说明符_Thread_local,与 externalinternal 链接或存储类说明符 static,具有 静态存储时长.

                  所以,如果 x 是全局的

                  int x;

                  那么它的存储时长、作用域和链接等价于

                  中的x

                  extern int x;

                  <块引用>

                  全局变量在范围内是否默认为static(或)相当于通过指定static存储类来声明变量?

                  没有.正如我上面所说,它的持续时间是 static 并且它具有 file 范围.

                  <块引用>

                  如果有任何 c 或 c++ 差异,请澄清?

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

                  I have 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. 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?

                  解决方案

                  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 还是等同于在 global 中使用 extern 声明变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何只执行一段代码一次? 下一篇:C++ 静态虚拟方法的替代方法

                  相关文章

                    <bdo id='zUn7s'></bdo><ul id='zUn7s'></ul>
                • <small id='zUn7s'></small><noframes id='zUn7s'>

                • <legend id='zUn7s'><style id='zUn7s'><dir id='zUn7s'><q id='zUn7s'></q></dir></style></legend>
                • <tfoot id='zUn7s'></tfoot>

                    1. <i id='zUn7s'><tr id='zUn7s'><dt id='zUn7s'><q id='zUn7s'><span id='zUn7s'><b id='zUn7s'><form id='zUn7s'><ins id='zUn7s'></ins><ul id='zUn7s'></ul><sub id='zUn7s'></sub></form><legend id='zUn7s'></legend><bdo id='zUn7s'><pre id='zUn7s'><center id='zUn7s'></center></pre></bdo></b><th id='zUn7s'></th></span></q></dt></tr></i><div id='zUn7s'><tfoot id='zUn7s'></tfoot><dl id='zUn7s'><fieldset id='zUn7s'></fieldset></dl></div>