C++中的保留字也称关键字,他们是预先定义好的字符集合,对C++编译有着特殊的含义。
标识符是程序员定义的有效字符序列,用来标识自己定义的变量名、函数名、数组名、类型名等。
C++ 关键字
关键字是预定义的单词,对编译器具有特殊的含义。例如,
int money;这里,int是一个关键字,表示money是整数类型的变量。
这是所有C ++关键字的列表。(从C ++ 17开始)
| alignas | decltype | namespace | struct |
| alignof | default | new | switch |
| and | delete | noexcept | template |
| and_eq | do | not | this |
| asm | double | not_eq | thread_local |
| auto | dynamic_cast | nullptr | throw |
| bitand | else | operator | true |
| bitor | enum | or | try |
| bool | explicit | or_eq | typedef |
| break | export | private | typeid |
| case | extern | protected | typename |
| catch | false | public | union |
| char | float | register | unsigned |
| char16_t | for | reinterpret_cast | using |
| char32_t | friend | return | virtual |
| class | goto | short | void |
| compl | if | signed | volatile |
| const | inline | sizeof | wchar_t |
| constexpr | int | static | while |
| const_cast | long | static_assert | xor |
| continue | mutable | static_cast | xor_eq |
注意:由于C ++是区分大小写的语言,因此所有关键字都必须用小写字母编写。
C++ 标识符
标识符是程序员给变量、类、函数或其他实体的唯一名称。例如,
int money;
double accountBalance;在这里,money和accountBalance是标识符。
标识符命名规则
标识符可以由字母,数字和下划线字符组成。
它对名称长度没有限制。
它必须以字母或下划线开头。
区分大小写。
我们不能将关键字用作标识符。
如果遵循上述规则,我们可以选择任何名称作为标识符。但是,我们应该为有意义的标识符提供有意义的名称。
好坏标识符示例
| 不合法的识别符 | 错误的标识符 | 好的标识符 |
|---|---|---|
| Total points | T_points | totalPoint |
| 1list | list_1 | list1 |
| float | n_float | floatNumber |