Dotcpp  >  编程教程  >  stdlib.h头文件  >  C语言atexit()函数:用于注册终止

C语言atexit()函数:用于注册终止

点击打开在线编译器,边学边练

函数名:atexit

头文件:<stdlib.h>

函数原型: int atexit(void (*func)(void)));

功能:用于注册终止

参数:func为指向终止处理函数的指针

返回值:成功  返回0值 ,失败  返回非0值


程序例:使用该函数实现程序终止时,调用注册出口函数f1和f2

#include<stdio.h>

#include<stdlib.h>

void f1();

void f2();

typedef void (*f)();  //定义函数指针类型

int main(void) {

   f fun1=f1,fun2=f2;  //定义函数指针

   atexit(fun1);

   atexit(fun2); 

   return 0;

}

void f1(){

   printf("exit first called\n");  

}

void f2(){

   printf("exit second called\n");

}

 

运行结果

exit second called
exit first called



本文固定URL:https://www.dotcpp.com/course/542

C语言函数库
assert.h头文件
ctype.h头文件
float.h头文件
io.h头文件
math.h头文件
mem.h头文件
setjmp.h头文件
stdio.h头文件
stdlib.h头文件
sigal.h头文件
string.h头文件
time.h头文件
Dotcpp在线编译      (登录可减少运行等待时间)