附录A C语言关键字速查表
2026/7/26 7:18:18 网站建设 项目流程

附录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>头文件,定义了更友好的布尔类型:

#include<stdbool.h>bool flag=true;// 等价于 _Bool flag = 1;flag=false;// 等价于 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. 关键字不能用作标识符

// ❌ 错误:关键字不能用作变量名intint=10;intreturn=5;intvoid=0;// ✅ 正确:使用其他名称intinteger=10;intret_value=5;intempty=0;

2. 关键字区分大小写

intx=10;// ✅ 正确:int是关键字Int y=20;// ✅ 正确:Int不是关键字(但不推荐这样命名)INT z=30;// ✅ 正确: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)。标准库会提供更友好的宏定义(如boolatomic_int)。

Q2: register关键字还有用吗?

:现代编译器的优化能力已经非常强大,register关键字基本上被忽略。编译器会自动决定哪些变量放入寄存器。

Q3: volatile有什么用?

volatile告诉编译器变量可能被外部因素改变(如硬件、中断、其他线程),防止编译器优化掉对该变量的访问。常用于:

  • 硬件寄存器映射
  • 信号处理函数中的全局变量
  • 多线程共享变量(但更推荐使用_Atomic

Q4: const和#define有什么区别?

  • const定义的是变量,有类型检查,占用内存
  • #define是预处理器宏,简单的文本替换,不占用内存
constintMAX=100;// 有类型的常量#defineMAX100// 宏定义

参考资料

  • ISO/IEC 9899:1990 (C90)
  • ISO/IEC 9899:1999 (C99)
  • ISO/IEC 9899:2011 (C11)

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询