附录A C语言关键字速查表

发布时间:2026/7/26 7:18:46
附录A C语言关键字速查表 附录A C语言关键字速查表概述C语言的关键字是语言的保留字具有特殊含义不能用作变量名、函数名或其他标识符。本附录列出了C语言各个标准中的所有关键字。C89/C90标准关键字32个这是最经典的C语言关键字集合所有C编译器都支持。数据类型关键字12个关键字说明示例char字符类型1字节char c A;int整数类型通常4字节int x 10;short短整型修饰符short s 100;long长整型修饰符long l 1000000L;float单精度浮点数4字节float f 3.14f;double双精度浮点数8字节double d 3.14159;signed有符号修饰符默认signed int x;unsigned无符号修饰符unsigned int u 100U;void空类型void func(void);struct结构体类型struct Point { int x, y; };union联合体类型union Data { int i; float f; };enum枚举类型enum Color { RED, GREEN, BLUE };存储类别关键字4个关键字说明示例auto自动存储类别默认很少使用auto int x;static静态存储类别static int count 0;extern外部变量声明extern int global_var;register寄存器存储类别建议register int i;控制流关键字12个关键字说明示例if条件判断if (x 0) { ... }elseif的否则分支if (x) ... else ...switch多分支选择switch (x) { case 1: ... }caseswitch分支标签case 1: printf(one); break;defaultswitch默认分支default: printf(other);forfor循环for (int i 0; i 10; i)whilewhile循环while (x 0) { ... }dodo-while循环do { ... } while (x);break跳出循环或switchbreak;continue跳过本次循环continue;goto无条件跳转不推荐goto label;return函数返回return 0;其他关键字4个关键字说明示例const常量修饰符const int MAX 100;volatile易变修饰符防止优化volatile int flag;sizeof计算类型或变量大小sizeof(int)typedef定义类型别名typedef int Integer;C99标准新增关键字5个C99标准引入了一些新特性和关键字。关键字说明示例inline内联函数建议inline int add(int a, int b) { return a b; }restrict指针限定符优化提示void func(int *restrict p);_Bool布尔类型1字节_Bool flag 1;_Complex复数类型double _Complex z 1.0 2.0*I;_Imaginary虚数类型double _Imaginary im 2.0*I;stdbool.h头文件C99提供了stdbool.h头文件定义了更友好的布尔类型#includestdbool.hbool flagtrue;// 等价于 _Bool flag 1;flagfalse;// 等价于 flag 0;C11标准新增关键字7个C11标准进一步扩展了C语言的功能。关键字说明示例_Alignas指定对齐方式_Alignas(16) int x;_Alignof查询对齐要求size_t align _Alignof(int);_Atomic原子类型线程安全_Atomic int counter 0;_Static_assert编译期断言_Static_assert(sizeof(int) 4, int must be 4 bytes);_Noreturn标记不返回的函数_Noreturn void exit(int status);_Thread_local线程局部存储_Thread_local int tls_var;_Generic泛型选择表达式#define abs(x) _Generic((x), int: abs, float: fabsf)(x)关键字使用规则1. 关键字不能用作标识符// ❌ 错误关键字不能用作变量名intint10;intreturn5;intvoid0;// ✅ 正确使用其他名称intinteger10;intret_value5;intempty0;2. 关键字区分大小写intx10;// ✅ 正确int是关键字Int y20;// ✅ 正确Int不是关键字但不推荐这样命名INT z30;// ✅ 正确INT不是关键字但不推荐这样命名3. 避免使用下划线开头的标识符虽然不是关键字但以下划线开头的标识符通常保留给编译器和标准库// ❌ 避免可能与编译器内部标识符冲突int_internal_var;int__reserved_name;// ✅ 推荐使用普通命名intinternal_var;intreserved_name;编译器扩展关键字不同编译器可能有自己的扩展关键字非标准GCC扩展__attribute__((packed))// 紧凑结构体__attribute__((aligned(16)))// 对齐__asm__// 内联汇编__typeof__// 类型推导MSVC扩展__declspec(dllexport)// DLL导出__declspec(align(16))// 对齐__int64// 64位整数__forceinline// 强制内联关键字速查卡片按功能分类类型相关charintshortlongfloatdoublesignedunsignedvoidstructunionenum_Bool_Complex_Imaginary存储类别autostaticexternregister_Thread_local类型修饰constvolatilerestrict_Atomic控制流ifelseswitchcasedefaultforwhiledobreakcontinuegotoreturn其他sizeoftypedefinline_Alignas_Alignof_Static_assert_Noreturn_Generic常见问题Q1: 为什么有些关键字以下划线开头答C标准委员会为了避免与现有代码冲突新增的关键字通常以下划线和大写字母开头如_Bool、_Atomic。标准库会提供更友好的宏定义如bool、atomic_int。Q2: register关键字还有用吗答现代编译器的优化能力已经非常强大register关键字基本上被忽略。编译器会自动决定哪些变量放入寄存器。Q3: volatile有什么用答volatile告诉编译器变量可能被外部因素改变如硬件、中断、其他线程防止编译器优化掉对该变量的访问。常用于硬件寄存器映射信号处理函数中的全局变量多线程共享变量但更推荐使用_AtomicQ4: const和#define有什么区别答const定义的是变量有类型检查占用内存#define是预处理器宏简单的文本替换不占用内存constintMAX100;// 有类型的常量#defineMAX100// 宏定义参考资料ISO/IEC 9899:1990 (C90)ISO/IEC 9899:1999 (C99)ISO/IEC 9899:2011 (C11)