C++ 关键字和标识符
本文整理了 C++ 的关键字和保留字,并介绍了标识符及其命名的规则。
在 C++ 中,关键字和保留字是编程语言的一部分,它们不能被用做标识符。C++ 也规定了标识符的命名规则。
C++ 关键字
关键字是对编译器具有特殊含义的预定义词。例如,
int money;
这里,int
是一个关键字,表示 money
是一个整数类型的变量。
这是从 C++17 开始所有 C++ 关键字的列表。()
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++ 是一种区分大小写的语言,所以请不要弄错大小写。
C++ 标识符
标识符是程序员为变量、类、函数或其他实体指定的唯一名称。例如,
int money;
double accountBalance;
这里, money
和 accountBalance
是标识符。
命名标识符的规则
- 标识符可以由字母、数字和下划线字符组成。
- 它必须以字母或下划线开头。
- 它区分大小写。
- 它对名称长度没有限制。
- 我们不能使用关键字作为标识符。
在遵循上述规则的前提下,我们可以选择任何名称作为标识符。但是,我们应该使用有意义的名称作为标识符。
标识符的例子
错误的标识符 | 不好的标识符 | 好的标识符 |
---|---|---|
Total points |
T_points |
totalPoint |
1list |
list_1 |
list1 |
float |
n_float |
floatNumber |