变量
作用:给一段指定的内存空间起名,方便操作这段内存
语法:数据类型 变量名 = 初始值;
示例
#includeusing namespace std; int main() { //变量的创建语法:数据类型 变量名 = 变量初始值 int a = 10; system("pause"); return 0; }
常量
作用:用于记录程序中不可更改的数据
C++定义常量两种方式
- #define宏常量:#define 常量名 常量值
- 通常在文件上方定义,表示一个常量
- cons修饰的变量:const 数据类型 变量名 常量值
- 通常在变量定义前加关键字const,修饰改变量为常量,不可更改
示例
#includeusing namespace std; // 宏常量 #define day 7 int main() { cout << "一周总共有:" << day << "天" << endl; // const修饰变量 const int year = 100; return 0; }
转载请注明出处❤️



