C 语言关键字
外观
C语言关键字[编辑 | 编辑源代码]
C语言关键字(也称为保留字)是C语言中具有特殊含义的预定义标识符,编译器将其用于特定语法结构的解析。这些关键字不能用作变量名、函数名或其他用户定义的标识符。C语言共有32个标准关键字(C89/C90标准),后续标准(如C99、C11)增加了少量新关键字。
概述[编辑 | 编辑源代码]
在C语言中,关键字是语言语法的基础组成部分,它们定义了数据类型、流程控制、存储类别等核心功能。所有关键字都是小写形式,编译器会将其识别为特殊标记。
关键字分类[编辑 | 编辑源代码]
C语言关键字可以按功能分为以下几类:
数据类型关键字[编辑 | 编辑源代码]
- 基本类型:
char
,int
,float
,double
- 类型修饰:
short
,long
,signed
,unsigned
- 复合类型:
struct
,union
,enum
- 类型定义:
typedef
- 空类型:
void
流程控制关键字[编辑 | 编辑源代码]
- 条件:
if
,else
,switch
,case
,default
- 循环:
for
,while
,do
- 跳转:
break
,continue
,return
,goto
存储类别关键字[编辑 | 编辑源代码]
auto
, register
, static
, extern
其他关键字[编辑 | 编辑源代码]
const
, volatile
, sizeof
详细说明与示例[编辑 | 编辑源代码]
数据类型关键字示例[编辑 | 编辑源代码]
#include <stdio.h>
int main() {
// 基本类型示例
int age = 25;
float height = 1.75f;
char initial = 'J';
// 类型修饰示例
unsigned int positiveNumber = 100;
long double preciseValue = 3.1415926535L;
printf("Age: %d, Height: %.2f, Initial: %c\n", age, height, initial);
printf("Unsigned: %u, Long double: %.10Lf\n", positiveNumber, preciseValue);
return 0;
}
输出:
Age: 25, Height: 1.75, Initial: J Unsigned: 100, Long double: 3.1415926535
流程控制关键字示例[编辑 | 编辑源代码]
#include <stdio.h>
int main() {
// if-else示例
int score = 85;
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else {
printf("Grade: C\n");
}
// switch-case示例
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good job!\n");
break;
default:
printf("Keep trying!\n");
}
return 0;
}
输出:
Grade: B Good job!
存储类别关键字示例[编辑 | 编辑源代码]
#include <stdio.h>
// 外部变量声明
extern int globalVar;
// 静态变量
static int counter = 0;
void increment() {
// 自动变量(auto默认可以省略)
auto int localVar = 0;
static int persistentVar = 0;
localVar++;
persistentVar++;
counter++;
printf("Local: %d, Persistent: %d, Counter: %d\n", localVar, persistentVar, counter);
}
int globalVar = 10;
int main() {
register int fastAccess = 5; // 建议寄存器存储
printf("Global: %d\n", globalVar);
printf("Register: %d\n", fastAccess);
increment();
increment();
increment();
return 0;
}
输出:
Global: 10 Register: 5 Local: 1, Persistent: 1, Counter: 1 Local: 1, Persistent: 2, Counter: 2 Local: 1, Persistent: 3, Counter: 3
关键字关系图[编辑 | 编辑源代码]
实际应用案例[编辑 | 编辑源代码]
使用const创建常量[编辑 | 编辑源代码]
#include <stdio.h>
// 使用const定义程序常量
const float PI = 3.14159f;
const int MAX_USERS = 100;
double calculateCircleArea(double radius) {
return PI * radius * radius;
}
int main() {
printf("PI: %.5f\n", PI);
printf("Max users: %d\n", MAX_USERS);
double r = 5.0;
printf("Area of circle (r=%.1f): %.2f\n", r, calculateCircleArea(r));
// PI = 3.14; // 错误!不能修改const变量
return 0;
}
输出:
PI: 3.14159 Max users: 100 Area of circle (r=5.0): 78.54
使用volatile处理硬件寄存器[编辑 | 编辑源代码]
// 假设这是一个嵌入式系统中的硬件寄存器
volatile unsigned int *statusRegister = (unsigned int *)0xFFFF0000;
void waitForStatusChange() {
while ((*statusRegister & 0x01) == 0) {
// 空循环,等待状态位变化
// volatile确保每次循环都从内存读取,不被优化
}
}
注意事项[编辑 | 编辑源代码]
1. 所有C语言关键字都是小写的,例如int
是关键字,而INT
不是。
2. 关键字不能重新定义或用作标识符。
3. 不同C标准新增的关键字:
* C99:_Bool
,_Complex
,_Imaginary
,inline
,restrict
* C11:_Alignas
,_Alignof
,_Atomic
,_Generic
,_Noreturn
,_Static_assert
,_Thread_local
数学公式示例[编辑 | 编辑源代码]
使用sizeof
关键字可以获取类型或对象的大小(以字节为单位):
总结[编辑 | 编辑源代码]
C语言关键字是构建程序的基本元素,理解每个关键字的用途和限制对于编写正确、高效的C代码至关重要。初学者应该熟悉所有标准关键字,并在实践中逐步掌握它们的用法。高级用户需要注意不同C标准引入的新关键字以及它们在特定场景下的优化用途。