目录
1 hello world
2 注释
3 变量
4 常量
5 c++关键字
6 标识符命名规则
1 hello world
#include
using namespace std;
int main()
{
cout << "hello world" << endl;
system("pause");
return 0;
}
2 注释
#include
using namespace std;
//单行注释
int main()
{
//在屏幕输出helloworld
system("pause");
return 0;
}
3 变量
#includeusing namespace std; //单行注释 int main() { //在屏幕输出helloworld system("pause"); return 0; }
3 变量
作用:给一般制定的内存空间起名,方便操作这段内存。
语法:数据类型 变量名 = 初始值;
#includeusing namespace std; int main2() { int a = 9; cout << "a = " << a << endl; system("pause"); return 0; }
4 常量
作用:用于记录程序中不可更改的数据。
c++定义常量的两种方式
1.#define宏常量:#define 常量名 常量值 通常在文件上方定义,表示一个常量。
2.const修饰的变量:const 数据类型 常量名 = 常量值 通常在变量定义前加关键字const,修饰该变量为常量,不可修改。
#includeusing namespace std; //#define宏常量 #define Day 7 int main() { //Day = 14; //常量不可修改,修改会报错。 cout << "一周一共有 " << Day << "天" << endl; //const修饰的变量 const int Month = 12; //Month = 24 //常量不可修改,修改会报错。 cout << "一年一共有 " << Month << "月" << endl; system("pause"); return 0; }
5 c++关键字
作用:关键字是c++中预先保留的单词(标识符)
在定义变量或者常量的时候,不要用关键字。
关键字如:asm、do、if、return、bool、int等等 c++关键字大集合
关键词做变量或常量报错:类型说明符的组合无效
6 标识符命名规则
作用:c++规定给标识符(变量、常量)命名时,有一套自己的规则。
- 标识符不能是关键字
- 标识符只能由字母、数字、下划线组成
- 第一个字符必须为字母或下划线
- 标识符中字母区分大小写



