在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 红包祝福语简短10字
18 周六早晨祝福语简短