变量的目的:
方便管理空间
语法:
数据类型 变量名 = 初始值
eg:int a = 10;
代码:
#include <iostream>
using namespace std;
int main()
{
int a = 10;
cout << "a=" << a << endl;
system("pause");
return 0;
}
常量目的:
记录不可更改的数据
1.#define宏定义(宏常量):
语法:
位置(文件上面)
#define 常量名 常量值
代码:
#include <iostream>
using namespace std;
#define math 98
int main()
{
cout << "数学成绩是:" << math << endl;
system("pause");
return 0;
}
2.const修饰的变量:
语法:
const 数据类型 常量名 = 常量值
位置(变量前加关键字const,修饰变量)
代码:
#include <iostream>
using namespace std;
int main()
{
const int math = 90;
cout << "数学成绩是:" << math << endl;
system("pause");
return 0;
}