在C和C++中,变量的const限定有何不同?
来自:Does "const" just mean read-only or something more?
“提示此问题的答案是:https://stackoverflow.com/questions/4024318#4024417,其中他声明const“just”在C中是只读的。我认为这就是const的意思,无论是C还是C++。他是什么意思?”
最佳答案
C语言中的const
不能用于构建常量表达式。
例如 :
#include <stdio.h>
int main()
{
int i = 2;
const int C = 2;
switch(i)
{
case C : printf("Hello") ;
break;
default : printf("World");
}
}
在C语言中不起作用,因为case标签不会减少为整数常量。