C++支持单行注释和多行注释。
// 注释内容
单行注释直到改行末尾,可以与代码放在同一行,在代码后面注释
/* 注释内容 */
包含在其中的都会被注释
变量的作用是给指定的内存空间起名,方便操作这段内存。变量值可以改变。
变量的使用可以分为三个阶段:
#include
using namespace std;
int main() {
int a = 10;
cout << "a = " << a << endl;
a = 20;
cout << "a = " << a << endl;
return 0;
}
输出
a = 10
a = 20
如果变量没有定义,没有初始化,那么使用该变量所得到的结果是不确定的。
常量与变量相对应,表示的是不可更改的数据。
C++中定义常量有两种方式


关键字是编译器保留字,不可用于作为常量名和变量名
C++关键字如下:
| asm | do | if | return | typedef |
|---|---|---|---|---|
| auto | double | inline | short | typeid |
| bool | dynamic_cast | int | signed | typename |
| break | else | long | sizeof | union |
| case | enum | mutable | static | unsigned |
| catch | explicit | namespace | static_cast | using |
| char | export | new | struct | virtual |
| class | extern | operator | switch | void |
| const | false | private | template | volatile |
| const_cast | float | protected | this | wchar_t |
| continue | for | public | throw | while |
| default | friend | register | true | |
| delete | goto | reinterpret_cast | try |
变量、常量这种标识符命名存在以下一些规则