在C全局变量是静态的或不? [英] global variable in C are static or not?

查看:161
本文介绍了在C全局变量是静态的或不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C 静态的extern 全局变量默认?结果
如果全局变量默认都是静态的,则意味着我们将能够访问它们在一个文件中,但我们可以用不同的文件中的全局变量为好。结果
这是否意味着他们在默认情况下外部存储方式?

Are global variables in C static or extern by default?
If global variables are by default static then 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 extern storage by default?

推荐答案

如果您不指定存储类(也就是的extern 静态关键字),则默认情况下全局变量的外部链接。从C99标准:

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

§6.2.2标识符联系

3)如果一个对象或函数的文件范围标识符的声明包含了存储类说明静态,该标识符有内部连接。

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)如果一个函数的标识符的声明没有存储类说明,其联动准确地确定就好像它是与存储类说明的extern 。如果一个对象标识符的声明有文件范围,无存储类说明,其联动是外部的。

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.

所以,即使你不指定的extern 关键字,全局还是可以通过其他的源文件(所谓的访问的翻译单元的),因为他们仍然可以有一个的extern 声明相同的变量。如果您使用静态关键字,即使在一个的extern 声明的presence指定内部连接,然后在另一个源文件相同的变量名,它指的一个不同的变量

So even if you don't specify the extern keyword, globals can still be accessed by other source files (so-called translation units), because they can still have an extern declaration for the same variable. If you use the static keyword to specify internal linkage, then even in the presence of an extern declaration for the same variable name in another source file, it will refer to a different variable.

这篇关于在C全局变量是静态的或不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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