在ANSI C中,const值是全局的吗?

use*_*315 1 c global const

我从一本C++书中读到,在ANSI C中,const值是全局的吗?但如果我声明一个const变量main()然后它将在本地作用域,那么我们怎么能说const是全局的呢?

oua*_*uah 5

C中没有全局词法范围,但有一个文件范围.const对象的限定不影响其范围.

#include <stdio.h>

int a = 0;         // file scope
const int b = 0;   // file scope

int main(void)
{
    int x;        // block scope
    const int y;  // block scope
}
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是,另外"a"和"b"变量都有_external linkage_,它通常被称为"全局变量".只有`static`关键字可以改变到内部的链接(但是只对在任何函数之外声明的变量,这些在内部声明没有链接),`const`在这方面不影响任何东西(以及在函数内声明的变量). (2认同)