C语言 C 中的全局变量是静态的还是非静态的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4239834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Global variables in C are static or not?
提问by Mishthi
Are global variables in C staticor externby default?
If global variables are by default staticthen it means that we would be able to access them in a single file, but we can use global variables in different files as well.
Does this imply that they have externstorage by default?
全局变量在 C 中static还是extern默认情况下?
如果全局变量是默认的,static那么这意味着我们可以在单个文件中访问它们,但我们也可以在不同的文件中使用全局变量。
这是否意味着它们extern默认具有存储空间?
回答by Adam Rosenfield
If you do not specify a storage class (that is, the externor statickeywords), then by default global variables have external linkage. From the C99 standard:
如果不指定存储类(即externorstatic关键字),则默认情况下全局变量具有外部链接。来自 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.
§6.2.2 标识符的链接
3) 如果对象或函数的文件范围标识符的声明包含存储类说明符
static,则该标识符具有内部链接。5) 如果函数的标识符声明没有存储类说明符,则其链接的确定与使用存储类说明符声明的完全相同
extern。如果对象的标识符声明具有文件范围且没有存储类说明符,则其链接是外部的。
So even if you don't specify the externkeyword, globals can still be accessed by other source files (so-called translation units), because they can still have an externdeclaration for the same variable. If you use the statickeyword to specify internal linkage, then even in the presence of an externdeclaration for the same variable name in another source file, it will refer to a different variable.
所以即使你不指定extern关键字,全局变量仍然可以被其他源文件(所谓的翻译单元)访问,因为它们仍然可以有extern相同变量的声明。如果您使用static关键字来指定内部链接,那么即使extern在另一个源文件中存在相同变量名的声明,它也会引用不同的变量。
回答by CB Bailey
In C, a global variable which doesn't have an initializer or any storage class specifiers is a tentative definition of a variable with static storage durationand external linkage.
在 C 中,没有初始化程序或任何存储类说明符的全局变量是具有静态存储持续时间和外部链接的变量的暂定定义。
In a translation unit all tentative definitions and up to one non-tentative definition (e.g. from a declaration with an initializer) are collapsed into a single definition for a variable. Although it's not allowed to have a definition of the same variable in multiple translation units it is a common extension to allow "common" variables, i.e. tentative definitions of the same variable in multiple translation units.
在翻译单元中,所有暂定定义和最多一个非暂定定义(例如来自带有初始化程序的声明)被折叠为变量的单个定义。虽然不允许在多个翻译单元中定义同一个变量,但允许“公共”变量是一个常见的扩展,即在多个翻译单元中同一个变量的暂定定义。
回答by Raghu Srikanth Reddy
Global variables in C are by default extern.. (i.e) they have external linkage..
C 中的全局变量默认为 extern ..(即)它们具有外部链接..
To restrict the external linkage, 'static' storage class specifier can be used for the global variable.. if static specifier is used, then the variable has file scope.. You cannot link it in an other file using the 'extern' keyword..
为了限制外部链接,'static' 存储类说明符可用于全局变量。如果使用静态说明符,则该变量具有文件范围。您不能使用 'extern' 关键字将其链接到其他文件中。 .
Specifying 'static' depends on your usage of the program..
指定“静态”取决于您对程序的使用。

