如果未在声明中初始化,则在c中使用const值有什么用?

pra*_*pta 10 c c++

可能重复:
C中的const与C++中的const

我有以下代码

在C.

int main()
{
    const int k;//allowed but garbage and later we can't modify
    printf("%d",k);
}
Run Code Online (Sandbox Code Playgroud)

O/P =垃圾

在C++中

int main()
{
    const int k; //not allowed from here itself
    printf("%d",k);
}
Run Code Online (Sandbox Code Playgroud)

o/p-compile时间错误

我怀疑constC中的用法是什么,如果它是allowed宣布它没有,initialization但在它之后declaration我们不能initialize它.

但是c++,如果没有,我们就不能宣布一个const价值initialization.

是否有任何变量k使用C或者它是无用的,如果我们只是声明它,因为以后的修改是不可能的.

pet*_*hen 9

它本身没有用处.

但是,有一些编译器特定的扩展,这将再次变得有用. C Compilers for embedded platforms例如,通常具有允许为变量提供固定地址的扩展,或者作为内存映射I/O端口的别名.

const将指示/强制您only read从该地址,例如amemory mapped input port.