1.c++ 变量、常量

c++中常量定义方式

1.#define 宏常量:#define 常量名 常量值

  • 通常在文件上方定义,表示一个常量

2.const修饰的变量:const 数据类型 常量名 = 常量值

  • 通常在变量定义前加关键之const,修饰该变量为常量,不可修改
#include <iostream>

using namespace std;

//1.宏常量 #define
#define Day 7

int main()
{
	//Day = 14;   这个是错误的,常量不能被修改
	cout << "一周有:" << Day << "天" << endl;


	//2.const 修饰的变量
	const int month = 12;	//const修饰的变量也是常量不能被修改

	cout << "一年一共有:" << month << "月" << endl;


	system("pause");

	return 0;
}
posted @ 2025-06-23 10:28  little小新  阅读(9)  评论(0)    收藏  举报