常量的作用:
用于记录程序中不可更改的数据
C++定义常量的方式有两种:
1、#define 宏常量
通常在文件上方定义
2、const 修饰的变量
通常在变量定义前加关键字 const
#include <iostream>
using namespace std;
//1、宏常量
#define Day 7
int main()
{
//Day = 8; //错误,Day是常量,一旦修改就会报错
cout << "day =" << Day << endl;
//2、const修饰的变量
const int month = 12;
//month = 24; //错误,const修饰的变量也称为常量
cout << "month=" << month << endl;
return 0;
}