Skip to content

Commit 07ae547

Browse files
committed
feat: Enhance documentation on C standard library macros
- Added comprehensive sections on standard library macros in C, including `<stddef.h>`, `<stdint.h>`, `<inttypes.h>`, `<limits.h>`, and `<float.h>`. - Introduced feature test macros for better compatibility across different C standards. - Updated examples and explanations to improve clarity and usability. - Removed outdated documentation files and corrected minor typos in existing examples. - Added a new transparent SVG file for visual representation in documentation.
1 parent ee73444 commit 07ae547

File tree

15 files changed

+648
-17
lines changed

15 files changed

+648
-17
lines changed

docs/.vitepress/nscfg.mts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export const Nav: DefaultTheme.NavItem[] = [
1212
text: "关于",
1313
link: "/About",
1414
},
15+
{
16+
text: "组件展示",
17+
link: "/杂项/组件展示",
18+
}
1519
];
1620
export const Sidebar: DefaultTheme.Sidebar = {
1721
"/教程/正文/": [
@@ -316,7 +320,7 @@ export const Sidebar: DefaultTheme.Sidebar = {
316320
},
317321
{
318322
text: "9.2 对象表示",
319-
link: "/教程/正文/语法和标准库/3_基本概念/3_1_注释",
323+
link: "/教程/正文/语法和标准库/9_对象/9_2_对象表示",
320324
collapsed: false,
321325
items: [
322326
{
@@ -448,7 +452,7 @@ export const Sidebar: DefaultTheme.Sidebar = {
448452
},
449453
{
450454
text: "12.6 功能特性测试宏",
451-
link: "/教程/正文/语法和标准库/12_标准库中的宏/12_6_预定义宏",
455+
link: "/教程/正文/语法和标准库/12_标准库中的宏/12_6_功能特性测试宏",
452456
},
453457
],
454458
},

docs/.vitepress/theme/style/var.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
--vp-c-neutral: var(--vp-c-black);
1010
--vp-c-neutral-inverse: var(--vp-c-white);
11+
1112
}
1213

1314
.dark {
@@ -540,6 +541,10 @@
540541

541542
--vp-home-hero-image-background-image: none;
542543
--vp-home-hero-image-filter: none;
544+
545+
--vp-home-hero-name-color: transparent;
546+
--vp-home-hero-name-background: -webkit-linear-gradient(120deg, #7b34fe 30%, #900095);
547+
--vp-home-hero-image-background-image: linear-gradient(-45deg, #9934fe 50%, #a10091 50%);
543548
}
544549

545550
/**

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ hero:
66
name: "Mdr-C-Tutorial"
77
text: "梦猫大典"
88
tagline: Free and Standard, Original and Dedicated
9+
image:
10+
src: /transparent.svg
911
actions:
1012
- theme: brand
1113
text: 正文

docs/public/transparent.svg

Lines changed: 2 additions & 0 deletions
Loading

docs/教程/标准文档翻译/目录.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
1.
44
2.
55
3. 术语,定义和符号
6-
4.
6+
4. 一致性
77
5. 环境
88
6. 语言
99
1.

docs/教程/正文/总纲.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
1. [**在开始编程之前**](/教程/正文/语法和标准库/1_在开始编程之前.md)
1414

1515
::: info todo
16+
17+
教程各章节顺序未完全确定,暂时不写这个列表。
18+
1619
:::
1720

1821
### 数据结构与算法模块
@@ -48,36 +51,37 @@
4851
1. 网络基础 - [**ISO 七层模型**](/教程/正文/项目/74_ISO七层模型.md)
4952
2. 网络基础 - [**TCP**](/教程/正文/项目/75_TCP.md)
5053
3. 网络基础 - [**UDP**](/教程/正文/项目/76_UDP.md)
51-
4. C 源码示例 - **kilo**
52-
5. 项目:多人聊天室 **Mdr Chat Room**
53-
6. C 源码示例 - **libuv**
54-
7. 项目:Web 服务器 **Mdr Server**
54+
4. **Linux 系统编程**
55+
5. C 源码示例 - **kilo**
56+
6. 项目:多人聊天室 **Mdr Chat Room**
57+
7. C 源码示例 - **libuv**
58+
8. 项目:Web 服务器 **Mdr Server**
5559
1. 网络基础 - **HTTP**
5660
2. 加密 - **TLS****SSL**
5761
3. 网络基础 - **HTTPS**
58-
8. 项目:编译型语言 **Mdr lang**
62+
9. 项目:编译型语言 **Mdr lang**
5963
1. 编译原理 - **词法分析**
6064
2. 编译原理 - **语法分析**
6165
3. 编译原理 - **语法制导翻译**
6266
4. 编译原理 - **中间代码生成**
6367
5. 编译器框架 - **LLVM**
6468
6. 编译原理 - **机器无关优化**
6569
7. (\*) 编译原理 - **运行时刻环境**
66-
9. 项目:数据库 **MdrDB**
67-
1. **关系型数据库**简介
68-
2. **SQL** 基础
69-
3. **SQL parser**
70-
4. **分页**存储
71-
5. C 源码示例:**SQLite**
72-
10. 项目:操作系统 **MdrOS**
70+
10. 项目:数据库 **MdrDB**
71+
1. **关系型数据库**简介
72+
2. **SQL** 基础
73+
3. **SQL parser**
74+
4. **分页**存储
75+
5. C 源码示例:**SQLite**
76+
11. 项目:操作系统 **MdrOS**
7377
1. [**引言**](/教程/正文/项目/MdrOS/项目概述.md)
7478
2. [**引导程序**](/教程/正文/项目/MdrOS/bootloader.md)
7579
3. [**视频驱动**](/教程/正文/项目/MdrOS/video_driver.md)
7680
4. [**中断系统**](/教程/正文/项目/MdrOS/interrupt.md)
7781
5. [**内存管理**](/教程/正文/项目/MdrOS/memory.md)
7882
6. [**文件系统**](/教程/正文/项目/MdrOS/filesystem.md)
7983
7. [**多任务**](/教程/正文/项目/MdrOS/task.md)
80-
11. 项目:硬件抽象层 **MdrHAL**
84+
12. 项目:硬件抽象层 **MdrHAL**
8185

8286
## 本教程分四部分
8387

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
# 标准库中的宏
2+
3+
在 C 语言编程中,****(preprocessor macros)是确保代码**可移植性****安全性****可维护性**的核心工具之一。标准库通过一系列头文件定义了大量预定义宏,这些宏提供平台无关的常量、类型别名、数值极限以及格式化说明符,帮助程序员避免硬编码假设,从而让代码在不同架构、编译器和操作系统上行为一致。
4+
5+
本教程将重点介绍C标准库中几组关键宏:
6+
7+
- **`<stddef.h>`**:基础宏,如 ` NULL `` offsetof `,以及类型 ` size_t ``ptrdiff_t`(常以宏形式间接使用)。
8+
- **`<stdint.h>`**:固定宽度整数类型(如 ` int32_t `)及其极限宏(如 ` INT32_MAX `` UINT64_MAX `)。
9+
- **`<inttypes.h>`**:格式化宏(如 ` PRId32 `` PRIu64 `` SCNxPTR `),用于`printf`/`scanf`家族的安全跨平台I/O。
10+
- **`<limits.h>`**:基本整数类型的数值极限(如 ` INT_MIN `` INT_MAX `` CHAR_BIT `` LLONG_MAX `)。
11+
- **`<float.h>`**:浮点类型的特性与极限(如 ` FLT_MAX `` DBL_EPSILON `` FLT_DIG `)。
12+
- **特征测试宏**:预定义宏如 ` __STDC_VERSION__ `` __STDC_HOSTED__ `,用于条件编译以适配不同C标准版本(C99、C11、C17、C23)和实现特性。
13+
14+
这些宏大多是**常量表达式**,可在预处理阶段、`#if` 指令或静态断言中使用。掌握它们,不仅能写出更健壮的代码,还能深入理解C语言的底层实现差异。
15+
16+
接下来,我们将逐一详解每个头文件中的宏、其定义目的、使用场景及示例,帮助你从基础到进阶全面运用这些强大工具。
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# `<stddef.h>`
2+
3+
**<stddef.h>** 定义了一些跨平台、跨实现的通用类型、宏和常量,几乎所有C程序都会直接或间接用到它们。
4+
5+
很多其他标准头文件(如`<stdlib.h>``<stdio.h>``<string.h>`等)都会隐式包含`<stddef.h>`,所以你经常不需要手动包含它。但当你直接使用其中的类型或宏时,最好显式包含,以提高代码可读性和可移植性。
6+
7+
## 如何使用
8+
9+
```c
10+
#include <stddef.h>
11+
```
12+
13+
## 1. size_t —— 表示对象大小的无符号整数类型
14+
15+
- **定义目的**`sizeof` 操作符的返回类型,也是很多函数(如`malloc``strlen`)用来表示大小或长度的参数类型。
16+
- **特点**:无符号整数,能表示任何对象可能的最大大小。
17+
- **典型实现**:通常是 `unsigned long long`
18+
- **示例**
19+
20+
```c
21+
#include <stddef.h>
22+
#include <stdio.h>
23+
24+
int main() {
25+
size_t size = sizeof(int); // 获取 int 类型占多少字节
26+
printf("int 的大小是:%zu 字节\n", size); // 注意:打印 size_t 用 %zu
27+
return 0;
28+
}
29+
```
30+
31+
可能的输出:
32+
33+
<TerminalWindow title="zsh - test_sizeof">
34+
35+
int 的大小是:4 字节
36+
37+
</TerminalWindow>
38+
39+
- **小贴士**:永远不要用 `int` 或 `unsigned int` 来代替`size_t`,这样能保证代码在不同平台上的可移植性。
40+
41+
## 2. ptrdiff_t —— 指针相减结果的带符号整数类型
42+
43+
- **定义目的**:两个指针相减的结果类型(例如数组中元素之间的距离)。
44+
- **特点**:带符号整数,能表示同一数组内任意两个指针的差值(包括负值)。
45+
- **典型实现**:通常是 `long` 或 `long long` 的有符号版本。
46+
- **示例**:
47+
48+
```c
49+
#include <stddef.h>
50+
#include <stdio.h>
51+
52+
int main() {
53+
int arr[] = {10, 20, 30, 40};
54+
int *p1 = &arr[0];
55+
int *p2 = &arr[3];
56+
ptrdiff_t diff = p2 - p1; // 结果可能是 3
57+
printf("指针差:%td\n", diff); // 打印 ptrdiff_t 用 %td
58+
return 0;
59+
}
60+
```
61+
62+
可能的输出:
63+
64+
<TerminalWindow title="zsh - test_ptrdiff">
65+
66+
指针差:3
67+
68+
</TerminalWindow>
69+
70+
- **小贴士**:在循环遍历数组时,常用 `ptrdiff_t` 作为索引差来避免溢出问题。
71+
72+
## 3. wchar_t —— 宽字符类型
73+
74+
- **定义目的**:用于表示宽字符(wide character),支持更大的字符集(如Unicode)。
75+
- **特点**:整数类型,足够大到能容纳任何支持的宽字符编码。
76+
- **示例**
77+
78+
```c
79+
#include <stddef.h>
80+
#include <wchar.h> // 宽字符函数通常在这里
81+
82+
int main() {
83+
wchar_t wc = L'中'; // 宽字符字面量用 L 前缀
84+
return 0;
85+
}
86+
```
87+
88+
## 4. max_align_t —— 最大对齐要求的标量类型(C11及以后引入)
89+
90+
- **定义目的**:一个对象类型,它的対齐要求是所有标量类型中最大的。
91+
- **用途**:在动态分配内存时,确保内存能满足最严格的对齐需求(比如用在 `aligned_alloc` 或自定义内存池)。
92+
- **示例**:
93+
94+
```c
95+
#include <stddef.h>
96+
#include <stdlib.h>
97+
98+
int main() {
99+
void *p = aligned_alloc(alignof(max_align_t), sizeof(max_align_t) * 10);
100+
// 分配的内存能满足最严格的对齐
101+
free(p);
102+
return 0;
103+
}
104+
```
105+
106+
- 这个功能在编写需要严格对齐的代码(如操作系统内核或嵌入式)时特别有用。
107+
108+
## 5. NULL —— 空指针常量
109+
110+
- **定义**:通常是`((void*)0)``0`
111+
- **用途**:表示指针不指向任何有效对象。
112+
- **示例**
113+
114+
```c
115+
#include <stddef.h>
116+
117+
int main() {
118+
int *p = NULL; // 安全初始化指针
119+
if (p == NULL) {
120+
// 指针为空
121+
}
122+
return 0;
123+
}
124+
```
125+
126+
- 注:C23 引入了 `nullptr`,在初始化指针的时候推荐写 `nulllptr`。
127+
128+
## 6. offsetof —— 计算结构体成员偏移的宏
129+
130+
- **定义**:`offsetof(type, member)` 返回成员`member`相对于结构体`type`起始地址的字节偏移量。
131+
- **特点**:结果类型是`size_t`。
132+
- **示例**:
133+
134+
```c
135+
#include <stddef.h>
136+
#include <stdio.h>
137+
138+
struct Student {
139+
int id;
140+
char name[20];
141+
double score;
142+
};
143+
144+
int main() {
145+
size_t offset = offsetof(struct Student, score);
146+
printf("score 成员的偏移量:%zu 字节\n", offset);
147+
return 0;
148+
}
149+
```
150+
151+
可能的输出:
152+
153+
<TerminalWindow title="zsh - test_offsetof">
154+
155+
score 成员的偏移量:24 字节
156+
157+
</TerminalWindow>
158+
159+
- **小贴士**:常用于调试、序列化、内存布局分析等场景。注意:不能用于有柔性数组成员或位域的复杂情况。
160+
161+
## 总结
162+
163+
`<stddef.h>` 虽然内容不多,但它是C语言的“基石”之一——它提供的类型和宏保证了代码的**可移植性****安全性**。在实际编程中:
164+
165+
- 几乎所有涉及大小、指针、内存布局的代码都会用到它。
166+
- 建议在需要这些类型时显式 `#include <stddef.h>`
167+
- 配合其他头文件(如 `<stdint.h>` 提供固定宽度整数,`<stdalign.h>` 提供对齐宏)使用,能写出更健壮的代码。
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# `<stdint.h>`
2+
3+
`<stdint.h>` 是 C99 引入的标准头文件,主要提供固定宽度整数类型、极限宏以及其他与整数表示相关的类型定义。目的是提高代码在不同平台间的可移植性,避免依赖实现定义的整数大小。
4+
5+
使用方式:
6+
7+
```c
8+
#include <stdint.h>
9+
```
10+
11+
## 1. 精确宽度整数类型(Exact-width integer types)
12+
13+
这些类型表示精确位宽的有符号和无符号整数。如果实现不支持对应宽度,则不定义该类型。
14+
15+
- 有符号:`int8_t``int16_t``int32_t``int64_t`
16+
- 无符号:`uint8_t``uint16_t``uint32_t``uint64_t`
17+
18+
示例:
19+
20+
```c
21+
int32_t x = -1000000; // 精确32位有符号整数
22+
uint8_t y = 255; // 精确8位无符号整数,范围0~255
23+
```
24+
25+
## 2. 最小宽度整数类型(Least-width integer types)
26+
27+
这些类型至少具有指定位宽,实现可提供更大宽度。
28+
29+
- 有符号:`int_least8_t``int_least16_t``int_least32_t``int_least64_t`
30+
- 无符号:`uint_least8_t``uint_least16_t``uint_least32_t``uint_least64_t`
31+
32+
## 3. 最快最小宽度整数类型(Fastest minimum-width integer types)
33+
34+
这些类型至少具有指定位宽,实现选择运算速度最快的类型(可能更大)。
35+
36+
- 有符号:`int_fast8_t``int_fast16_t``int_fast32_t``int_fast64_t`
37+
- 无符号:`uint_fast8_t``uint_fast16_t``uint_fast32_t``uint_fast64_t`
38+
39+
## 4. 指针整数类型(Integer types capable of holding object pointers)
40+
41+
- `intptr_t`:有符号整数类型,可容纳转换后的指针值(可选)。
42+
- `uintptr_t`:无符号整数类型,可容纳转换后的指针值(可选)。
43+
44+
示例:
45+
46+
```c
47+
void *ptr = ...;
48+
uintptr_t addr = (uintptr_t)ptr; // 将指针转换为整数
49+
```
50+
51+
## 5. 最大宽度整数类型(Greatest-width integer types)
52+
53+
- `intmax_t`:有符号整数类型,至少与任何其他有符号整数类型一样宽。
54+
- `uintmax_t`:无符号整数类型,至少与任何其他无符号整数类型一样宽。
55+
56+
## 6. 极限宏(Limits macros)
57+
58+
为上述所有类型定义最小值和最大值宏,例如:
59+
60+
- `INT8_MIN``INT8_MAX``UINT8_MAX`
61+
- `INT32_MIN``INT32_MAX``UINT32_MAX`
62+
- 同理适用于16/32/64位以及least/fast/max类型。
63+
64+
这些宏在 `<limits.h>` 中也有部分重叠,但 `<stdint.h>` 提供针对固定宽度类型的完整集合。
65+
66+
## 7. 其他宏
67+
68+
- `PTRDIFF_MIN``PTRDIFF_MAX``ptrdiff_t`的极限(与`<stddef.h>` 协调)。
69+
- `SIZE_MAX``size_t` 的最大值。
70+
- `SIG_ATOMIC_MIN``SIG_ATOMIC_MAX``sig_atomic_t`的极限。
71+
- `WCHAR_MIN``WCHAR_MAX``wchar_t` 的极限。
72+
- `WINT_MIN``WINT_MAX``wint_t` 的极限。
73+
74+
## 格式化输出
75+
76+
这些类型的格式化输出需配合 `<inttypes.h>` 中的宏(如 `PRId32` 用于`printf("%" PRId32, x);`)。

0 commit comments

Comments
 (0)