C++ 学习笔记 (3)—— 常量

作用:

用于记录程序中不可更改的数据

C++中定义常量的两种方式:

1.#define 宏常量 :#define 常量名 常量值 通常在文件上方定义,表示一个常量

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

通常在变量定义前加关键字const,修饰变量为常量,不可修改

 

#include <iostream>

using namespace std; //常量的定义方式

//1.#define 宏常量

//2.const 修饰的变量

#define Day 7

int main()

{

cout << "一周有:"<< Day << "天"<< endl;

//Day = 10; //Day 是常量 不能再修改值

const int imonth = 12;

//imonth = 12; //const 修饰的变量也称为常量,不可以再修改

cout <<"一年有:"<< imonth << "个月分" << endl;

system("pause");

return 0;

}

posted @ 2022-04-10 22:09  雾枫  阅读(38)  评论(0编辑  收藏  举报