C中的全局变量是静态的还是不是?

默认情况下,C静态或外部的全局变量是?
如果全局变量默认是静态的,那么它意味着我们可以在单个文件中访问它们,但我们也可以在不同的文件中使用全局变量.
这是否意味着默认情况下它们具有外部存储?

解决方法

如果未指定存储类(即extern或static关键字),则默认情况下全局变量具有外部链接.从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.

因此,即使您没有指定extern关键字,仍然可以通过其他源文件(所谓的转换单元)访问全局变量,因为它们仍然可以对同一变量进行extern声明.如果使用static关键字指定内部链接,那么即使在另一个源文件中存在相同变量名的extern声明,它也会引用另一个变量.

相关文章

一.C语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...