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 |
双精度浮点数 |
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 |
浮点数 |
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 |
浮点数 | n_float | floatNumber |