常量
用于记录程序中不可更改的数据
#define 宏常量
-
通常在文件上方定义,表示一个常量
使用方式
xxxxxxxxxx
例如
xxxxxxxxxx
using namespace std;
int main() {
cout << "a = " << a << endl;
system("pause");
return 0;
}
运行

const 修饰的变量
-
通常在变量定义前加关键字 const ,修饰该变量为常量
使用方式
xxxxxxxxxx
const 数据类型 常量名 = 常量值
例如
xxxxxxxxxx
using namespace std;
int main() {
const int a = 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
运行

注意
如果遇见错误 "应为表达式"

则需要检查, #define 中结尾是否右分号

将分号删除即可消除错误
