C语言中定义的全局变量是静态的还是非静态的?

2026-04-16 22:523阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计394个文字,预计阅读时间需要2分钟。

C语言中定义的全局变量是静态的还是非静态的?

在默认情况下,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语言中定义的全局变量是静态的还是非静态的?

本文共计394个文字,预计阅读时间需要2分钟。

C语言中定义的全局变量是静态的还是非静态的?

在默认情况下,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语言中定义的全局变量是静态的还是非静态的?