File tree Expand file tree Collapse file tree 1 file changed +40
-1
lines changed
Expand file tree Collapse file tree 1 file changed +40
-1
lines changed Original file line number Diff line number Diff line change 11# 执行控制函数
22
3- C标准库提供了一系列用于程序执行控制的函数 ,这些函数主要定义在 ` stdlib.h ` 头文件中。主要包括程序终止、系统命令执行以及环境控制等功能。
3+ C 标准库提供了一系列用于程序执行控制的函数 ,这些函数主要定义在 ` stdlib.h ` 头文件中。主要包括程序终止、系统命令执行以及环境控制等功能。
44
55## 程序终止函数
66
@@ -10,12 +10,47 @@ C标准库提供了一系列用于程序执行控制的函数,这些函数主
1010int atexit ( void (* func)(void) );
1111```
1212
13+ 注册函数。被注册的函数将在程序正常终止时被逆序调用。同一个函数可以被注册多次。
14+
15+ 注:注意到函数原型中的参数,这说明被注册的函数形参列表必须为 `void`,且没有返回值。
16+
17+ 返回值:成功时返回 `0`,失败时返回非零值。
18+
19+ 示例:
20+
21+ ```c
22+ #include <stdlib.h>
23+ #include <stdio.h>
24+
25+ void f1(void) { puts("f1"); }
26+ void f2(void) { puts("f2"); }
27+
28+ int main(void) {
29+ if ( ! atexit(f1) && ! atexit(f2) && ! atexit(f2) ) {
30+ return EXIT_SUCCESS ;
31+ }
32+ return EXIT_FAILURE ;
33+ }
34+ ```
35+
36+ 可能的输出:
37+
38+ ``` txt
39+ f2
40+ f2
41+ f1
42+ ```
43+
1344### ` at_quick_exit ` 函数
1445
1546``` c
1647int at_quick_exit ( void (* func)(void) ); // C11 起
1748```
1849
50+ 大部分特性类似于 `atexit()`。通过它注册的函数在程序快速终止(即调用 `quick_exit()`)时被调用。
51+
52+ `at_quick_exit()` 函数是线程安全的。
53+
1954### `exit` 函数
2055
2156```c
@@ -48,3 +83,7 @@ void abort(void);
4883_Noreturn void abort(void); // C11 / C17
4984[[noreturn]] void abort(void); // C23 起
5085```
86+
87+ ## 系统命令执行
88+
89+ ### ` system ` 函数
You can’t perform that action at this time.
0 commit comments