当前位置: 首页 > news >正文

C语言关键字详解

C语言关键字详解

    • 1. 数据类型关键字
      • 1.1 基本类型关键字
      • 1.2 类型修饰符关键字
    • 2. 复杂类型关键字
    • 3. 存储类别关键字
    • 4. 控制流关键字
      • 4.1 条件语句关键字
      • 4.2 循环和跳转关键字
    • 5. 其他重要关键字
    • 6. C99新增关键字
    • 7. 完整的关键字分类总结
    • 8. 使用注意事项

1. 数据类型关键字

1.1 基本类型关键字

关键字作用说明
int声明整型变量,通常占用4字节
char声明字符型变量,占用1字节
float声明单精度浮点型变量,占用4字节
double声明双精度浮点型变量,占用8字节
void表示无类型,用于函数返回值或指针
#include <stdio.h>int main() {// int - 整型int age = 25;printf("年龄: %d\n", age);// char - 字符型char grade = 'A';printf("等级: %c\n", grade);// float - 单精度浮点型float price = 19.99f;printf("价格: %.2f\n", price);// double - 双精度浮点型double salary = 50000.75;printf("工资: %.2lf\n", salary);// void - 无类型void displayMessage(); // 函数声明return 0;
}// void 函数示例
void displayMessage() {printf("这是一个无返回值的函数\n");
}

1.2 类型修饰符关键字

关键字作用说明
short修饰整型,表示短整型,占用2字节
long修饰整型,表示长整型,占用4或8字节
signed表示有符号类型,可以存储正负数(默认)
unsigned表示无符号类型,只能存储非负数
#include <stdio.h>int main() {// short - 短整型short smallNumber = 100;printf("短整型: %hd\n", smallNumber);// long - 长整型long bigNumber = 123456789L;printf("长整型: %ld\n", bigNumber);// signed - 有符号类型(默认)signed int temperature = -10;printf("温度: %d\n", temperature);// unsigned - 无符号类型unsigned int distance = 300;printf("距离: %u\n", distance);return 0;
}

2. 复杂类型关键字

关键字作用说明
struct定义结构体类型,将不同类型的数据组合在一起
union定义联合体类型,所有成员共享同一内存空间
enum定义枚举类型,为一组整数值提供有意义的名称
typedef为现有类型创建新的类型别名
#include <stdio.h>// struct - 结构体
struct Student {char name[50];int age;float score;
};// union - 联合体
union Data {int i;float f;char str[20];
};// enum - 枚举
enum Color {RED,GREEN,BLUE
};// typedef - 类型定义
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);// typedef 使用uint count = 100;printf("计数: %u\n", count);return 0;
}

3. 存储类别关键字

关键字作用说明
auto自动存储期,在块开始时分配,结束时释放(默认)
static静态存储期,生命周期贯穿整个程序运行期间
register建议编译器将变量存储在寄存器中以提高访问速度
extern声明变量或函数在其他文件中定义
#include <stdio.h>// auto - 自动变量(默认,通常省略)
void autoExample() {auto int x = 10; // 等同于 int x = 10;printf("自动变量: %d\n", x);
}// static - 静态变量
void staticExample() {static int count = 0; // 只初始化一次count++;printf("静态变量计数: %d\n", count);
}// register - 寄存器变量
void registerExample() {register int i; // 建议编译器将变量存储在寄存器中for(i = 0; i < 5; i++) {printf("寄存器变量: %d\n", i);}
}// extern - 外部变量
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-elseif(score >= 90) {printf("优秀\n");} else if(score >= 60) {printf("及格\n");} else {printf("不及格\n");}// switch-caseint 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;// for 循环printf("for 循环: ");for(i = 0; i < 5; i++) {printf("%d ", i);}printf("\n");// while 循环printf("while 循环: ");i = 0;while(i < 5) {printf("%d ", i);i++;}printf("\n");// do-while 循环printf("do-while 循环: ");i = 0;do {printf("%d ", i);i++;} while(i < 5);printf("\n");// break 和 continueprintf("break 和 continue 示例: ");for(i = 0; i < 10; i++) {if(i == 2) continue; // 跳过2if(i == 7) break;    // 在7处退出循环printf("%d ", i);}printf("\n");return 0;
}

5. 其他重要关键字

关键字作用说明
const定义常量,值在程序运行期间不可改变
volatile表示变量可能被意外修改,阻止编译器优化
sizeof运算符,返回类型或对象的大小(字节数)
#include <stdio.h>
#include <stdlib.h>// const - 常量
const int MAX_SIZE = 100;// volatile - 易变变量
volatile int sensorValue;// sizeof - 获取类型或变量的大小
void sizeExample() {printf("int 大小: %zu 字节\n", sizeof(int));printf("double 大小: %zu 字节\n", sizeof(double));int arr[10];printf("数组大小: %zu 字节\n", sizeof(arr));
}// return - 函数返回值
int add(int a, int b) {return a + b;
}// goto - 跳转语句(一般不推荐使用)
void gotoExample() {int i = 0;loop:printf("%d ", i);i++;if(i < 5) {goto loop;}printf("\n");
}int main() {// const 使用printf("最大大小: %d\n", MAX_SIZE);// sizeof 使用sizeExample();// return 使用int result = add(5, 3);printf("5 + 3 = %d\n", result);// goto 使用gotoExample();return 0;
}

6. C99新增关键字

关键字作用说明
_BoolC99引入的布尔类型,只能存储0或1
_ComplexC99引入的复数类型
_ImaginaryC99引入的虚数类型
restrict限制指针,表明指针是访问数据的唯一方式
inline建议编译器将函数内联展开
#include <stdio.h>
#include <stdbool.h>// _Bool - 布尔类型 (C99)
// bool - 在stdbool.h中定义
void boolExample() {_Bool isTrue = 1;  // C99布尔类型bool isFalse = false; // 通过stdbool.hprintf("_Bool 值: %d\n", isTrue);printf("bool 值: %d\n", isFalse);
}// _Complex - 复数类型 (C99)
#include <complex.h>
void complexExample() {double complex z1 = 1.0 + 2.0 * I; // 复数 1+2iprintf("复数: %.1f + %.1fi\n", creal(z1), cimag(z1));
}// restrict - 限制指针 (C99)
void copyArray(int *restrict dest, const int *restrict src, int n) {for(int i = 0; i < n; i++) {dest[i] = src[i];}
}// inline - 内联函数 (C99)
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. 使用注意事项

  1. 关键字不能用作标识符:所有关键字都是保留字,不能用作变量名、函数名等
  2. 大小写敏感:C语言关键字都是小写的
  3. 标准一致性:不同C标准(C89、C99、C11)支持的关键字可能有所不同
  4. 编译器扩展:某些编译器可能提供额外的关键字(如__asm__attribute__等)
http://www.dtcms.com/a/589686.html

相关文章:

  • SmartResume简历信息抽取框架深度解析
  • 2.4 使用 PyTorch / TensorFlow 实现文本分类
  • 传统企业网站建设运营分析闵行区个人网页设计用户体验
  • 从随机变量到统计模型(二)
  • AIGC|北京AI优化企业新榜单与选择指南
  • 站长工具怎么用北京建网站 优帮云
  • files-to-prompt 简介
  • 计算机软件开发网站建设取什么名字营销型外贸网站定制
  • 实战:搭建一个简单的股票价格监控和警报系统
  • 在 Unreal VR 项目中用双目立体全景天空盒优化性能与沉浸感
  • 专业网站推广引流电子商务网站建设与实例
  • 个人免费网站申请关键词挖掘查询工具
  • 学习周报二十一
  • 公司网站建设劳伦网店代运营收费
  • 云南 旅游 网站建设山东嘉邦家居用品公司网站 加盟做经销商多少钱 有人做过吗
  • 触摸未来2025-11-09:万有力,图论革命
  • 做物流哪个网站推广效果好外贸网站的推广技巧有哪些
  • 舞钢市城市建设局网站模板王字体网
  • C++14常用新特性
  • 使用n8n搭建服务器监控系统:从Webhook到Telegram告警的完整实现
  • 如何在Dev-C++中启用调试模式?
  • 高校两学一做网站建设装修公司网站建设设计作品
  • Linux复习:操作系统管理本质:“先描述,再组织”,贯穿软硬件的核心思想
  • 简历电商网站开发经验介绍互联网网站类型
  • C#项目 无法附加到进程。已附加了一个调试器。
  • K8s Overlay 网络:核心原理、主流方案与实践指南
  • 莆田市网站建设网站建设英文版
  • ULUI:不止于按钮和菜单,一个专注于“业务组件”的纯 CSS 框架
  • 怎么自己做礼品网站一份电子商务网站建设规划书
  • InnoDB 与 MyISAM 的底层区别与选择策略