C语言关键字详解
- 1. 数据类型关键字
- 2. 复杂类型关键字
- 3. 存储类别关键字
- 4. 控制流关键字
- 5. 其他重要关键字
- 6. C99新增关键字
- 7. 完整的关键字分类总结
- 8. 使用注意事项
1. 数据类型关键字
1.1 基本类型关键字
| 关键字 | 作用说明 |
|---|
int | 声明整型变量,通常占用4字节 |
char | 声明字符型变量,占用1字节 |
float | 声明单精度浮点型变量,占用4字节 |
double | 声明双精度浮点型变量,占用8字节 |
void | 表示无类型,用于函数返回值或指针 |
#include <stdio.h>int main() {int age = 25;printf("年龄: %d\n", age);char grade = 'A';printf("等级: %c\n", grade);float price = 19.99f;printf("价格: %.2f\n", price);double salary = 50000.75;printf("工资: %.2lf\n", salary);void displayMessage(); return 0;
}
void displayMessage() {printf("这是一个无返回值的函数\n");
}
1.2 类型修饰符关键字
| 关键字 | 作用说明 |
|---|
short | 修饰整型,表示短整型,占用2字节 |
long | 修饰整型,表示长整型,占用4或8字节 |
signed | 表示有符号类型,可以存储正负数(默认) |
unsigned | 表示无符号类型,只能存储非负数 |
#include <stdio.h>int main() {short smallNumber = 100;printf("短整型: %hd\n", smallNumber);long bigNumber = 123456789L;printf("长整型: %ld\n", bigNumber);signed int temperature = -10;printf("温度: %d\n", temperature);unsigned int distance = 300;printf("距离: %u\n", distance);return 0;
}
2. 复杂类型关键字
| 关键字 | 作用说明 |
|---|
struct | 定义结构体类型,将不同类型的数据组合在一起 |
union | 定义联合体类型,所有成员共享同一内存空间 |
enum | 定义枚举类型,为一组整数值提供有意义的名称 |
typedef | 为现有类型创建新的类型别名 |
#include <stdio.h>
struct Student {char name[50];int age;float score;
};
union Data {int i;float f;char str[20];
};
enum Color {RED,GREEN,BLUE
};
typedef unsigned int uint;int main() {struct Student stu1 = {"张三", 20, 85.5};printf("学生: %s, 年龄: %d, 分数: %.1f\n", stu1.name, stu1.age, stu1.score);union Data data;data.i = 10;printf("联合体整数值: %d\n", data.i);enum Color favorite = BLUE;printf("最喜欢的颜色编号: %d\n", favorite);uint count = 100;printf("计数: %u\n", count);return 0;
}
3. 存储类别关键字
| 关键字 | 作用说明 |
|---|
auto | 自动存储期,在块开始时分配,结束时释放(默认) |
static | 静态存储期,生命周期贯穿整个程序运行期间 |
register | 建议编译器将变量存储在寄存器中以提高访问速度 |
extern | 声明变量或函数在其他文件中定义 |
#include <stdio.h>
void autoExample() {auto int x = 10; printf("自动变量: %d\n", x);
}
void staticExample() {static int count = 0; count++;printf("静态变量计数: %d\n", count);
}
void registerExample() {register int i; for(i = 0; i < 5; i++) {printf("寄存器变量: %d\n", i);}
}
extern int globalVar; int globalVar = 100; int main() {autoExample();staticExample();staticExample(); registerExample();printf("外部变量: %d\n", globalVar);return 0;
}
4. 控制流关键字
4.1 条件语句关键字
| 关键字 | 作用说明 |
|---|
if | 条件判断语句的开始 |
else | 与if配合使用,表示条件不成立时执行的代码 |
switch | 多分支选择语句的开始 |
case | 在switch中定义具体的分支条件 |
default | 在switch中定义默认分支 |
#include <stdio.h>int main() {int score = 85;if(score >= 90) {printf("优秀\n");} else if(score >= 60) {printf("及格\n");} else {printf("不及格\n");}int day = 3;switch(day) {case 1:printf("星期一\n");break;case 2:printf("星期二\n");break;case 3:printf("星期三\n");break;default:printf("其他天\n");}return 0;
}
4.2 循环和跳转关键字
| 关键字 | 作用说明 |
|---|
for | 循环语句,包含初始化、条件和迭代部分 |
while | 循环语句,先判断条件再执行循环体 |
do | 与while配合,先执行循环体再判断条件 |
break | 跳出当前循环或switch语句 |
continue | 跳过当前循环的剩余代码,进入下一次循环 |
goto | 无条件跳转到指定标签(一般不推荐使用) |
return | 从函数返回,并可返回一个值 |
#include <stdio.h>int main() {int i;printf("for 循环: ");for(i = 0; i < 5; i++) {printf("%d ", i);}printf("\n");printf("while 循环: ");i = 0;while(i < 5) {printf("%d ", i);i++;}printf("\n");printf("do-while 循环: ");i = 0;do {printf("%d ", i);i++;} while(i < 5);printf("\n");printf("break 和 continue 示例: ");for(i = 0; i < 10; i++) {if(i == 2) continue; if(i == 7) break; printf("%d ", i);}printf("\n");return 0;
}
5. 其他重要关键字
| 关键字 | 作用说明 |
|---|
const | 定义常量,值在程序运行期间不可改变 |
volatile | 表示变量可能被意外修改,阻止编译器优化 |
sizeof | 运算符,返回类型或对象的大小(字节数) |
#include <stdio.h>
#include <stdlib.h>
const int MAX_SIZE = 100;
volatile int sensorValue;
void sizeExample() {printf("int 大小: %zu 字节\n", sizeof(int));printf("double 大小: %zu 字节\n", sizeof(double));int arr[10];printf("数组大小: %zu 字节\n", sizeof(arr));
}
int add(int a, int b) {return a + b;
}
void gotoExample() {int i = 0;loop:printf("%d ", i);i++;if(i < 5) {goto loop;}printf("\n");
}int main() {printf("最大大小: %d\n", MAX_SIZE);sizeExample();int result = add(5, 3);printf("5 + 3 = %d\n", result);gotoExample();return 0;
}
6. C99新增关键字
| 关键字 | 作用说明 |
|---|
_Bool | C99引入的布尔类型,只能存储0或1 |
_Complex | C99引入的复数类型 |
_Imaginary | C99引入的虚数类型 |
restrict | 限制指针,表明指针是访问数据的唯一方式 |
inline | 建议编译器将函数内联展开 |
#include <stdio.h>
#include <stdbool.h>
void boolExample() {_Bool isTrue = 1; bool isFalse = false; printf("_Bool 值: %d\n", isTrue);printf("bool 值: %d\n", isFalse);
}
#include <complex.h>
void complexExample() {double complex z1 = 1.0 + 2.0 * I; printf("复数: %.1f + %.1fi\n", creal(z1), cimag(z1));
}
void copyArray(int *restrict dest, const int *restrict src, int n) {for(int i = 0; i < n; i++) {dest[i] = src[i];}
}
inline int max(int a, int b) {return (a > b) ? a : b;
}int main() {boolExample();complexExample();int src[5] = {1, 2, 3, 4, 5};int dest[5];copyArray(dest, src, 5);printf("最大值: %d\n", max(10, 20));return 0;
}
7. 完整的关键字分类总结
| 类别 | 关键字 | 主要用途 |
|---|
| 基本类型 | int, char, float, double, void | 定义基本数据类型 |
| 类型修饰符 | short, long, signed, unsigned | 修饰数据类型的大小和符号性 |
| 复杂类型 | struct, union, enum, typedef | 定义复杂数据类型和类型别名 |
| 存储类别 | auto, static, register, extern | 控制变量的存储方式、生命周期和可见性 |
| 条件控制 | if, else, switch, case, default | 实现条件分支逻辑 |
| 循环控制 | for, while, do | 实现循环逻辑 |
| 跳转控制 | break, continue, goto, return | 改变程序执行流程 |
| 其他关键字 | const, volatile, sizeof | 定义常量、易变变量和获取大小 |
| C99新增 | _Bool, _Complex, restrict, inline | 布尔类型、复数类型、指针限制和内联函数 |
8. 使用注意事项
- 关键字不能用作标识符:所有关键字都是保留字,不能用作变量名、函数名等
- 大小写敏感:C语言关键字都是小写的
- 标准一致性:不同C标准(C89、C99、C11)支持的关键字可能有所不同
- 编译器扩展:某些编译器可能提供额外的关键字(如
__asm、__attribute__等)