How does quot;constquot; differ in C and C++?(“const如何?C 和 C++ 的区别?)
问题描述
变量的 const 限定在 C 和 C++ 中有何不同?
How does the const qualification on variables differ in C and C++?
来自:是否const"只是意味着只读还是更多?
引发这个问题的是这个答案:https://stackoverflow.com/questions/4024318#4024417 其中他说 const just"在 C 中表示只读.我认为这就是 const 的全部含义,无论是 C 还是 C++.他是什么意思?"
"What prompted this question was this answer: https://stackoverflow.com/questions/4024318#4024417 where he states const "just" means read-only in C. I thought that's all const meant, regardless of whether it was C or C++. What does he mean?"
推荐答案
const 在 C 中不能用于构建常量表达式.
const in C cannot be used to build constant expressions.
例如:
#include <stdio.h>
int main()
{
int i = 2;
const int C = 2;
switch(i)
{
case C : printf("Hello") ;
break;
default : printf("World");
}
}
在 C 中不起作用,因为 case 标签不会减少为整数常量.
doesn't work in C because case label does not reduce to an integer constant.
这篇关于“const"如何?C 和 C++ 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“const"如何?C 和 C++ 的区别?
基础教程推荐
- C++实战之二进制数据处理与封装 2023-05-29
- 全面了解C语言 static 关键字 2023-03-26
- 带你深度走入C语言取整以及4种函数 2022-09-17
- [c语言-函数]不定量参数 2023-09-08
- centos 7 vscode cmake 编译c++工程 2023-09-17
- C++实现ETW进行进程变动监控详解 2023-05-15
- C语言编程C++旋转字符操作串示例详解 2022-11-20
- C语言 详解字符串基础 2023-03-27
- [C语言]二叉搜索树 2023-09-07
- C语言实现宾馆管理系统课程设计 2023-03-13
