在C中main()之前和之后执行的函数
在这里,我们将看到如何编写存在两个功能的代码,其中一个功能将在主功能之前执行,而另一个功能将在主功能之后执行。这些功能用于在执行main之前执行一些启动任务,并在执行main之后执行一些清理任务。
为此,我们必须为这两个函数放置属性。当属性是构造函数属性时,它将在执行之前main(),而当属性是析构函数类型时,它将在之后执行main()。
范例程式码
#include<stdio.h>
void before_main() __attribute__((constructor));
void after_main() __attribute__((destructor));
void before_main() {
printf("This is executed before main.\n");
}
void after_main() {
printf("这是在main之后执行的。");
}
main() {
printf("Inside main\n");
}输出结果
This is executed before main. Inside main 这是在main之后执行的。
热门推荐
10 小学毕业父母简短祝福语
11 幼儿生日祝福语模板简短
12 新娘妈妈的祝福语简短
13 邻居聚会祝福语大全简短
14 十条祝福语简短
15 给同学祝福语的简短
16 过年祝福语简短祝妈妈
17 祝女儿简短祝福语大全
18 恩师诗词祝福语简短大全