c++ 常量

74 阅读1分钟

作用

常量就像是常规的变量,只不过常量的值在定义后不能进行修改,在程序执行期间不会进行改变。用于记录程序中不可更改的数据。

语法

  • 宏常量: #define 常量名 常量值
  • const修饰的变量:const 数据类型 常量名 = 常量值

示例

#include<iostream>
using namespace std;

// 宏常量
// #define 常量名 常量值
#define week 7 // 一周为7天

int main() {

	// const修饰的变量
	// const 数据类型 常量名 = 常量值
	const int minute = 60; // 一分钟为60秒

	cout << "一周 = " << week << "天" << endl;
	cout << "一分钟 = " << minute << "秒" << endl;

	system("pause");
	return 0;
}