atexit()函数--接受一个函数指针作为一个参数(调用exit函数,便会执行atexit函数)

#include <stdio.h>
#include <stdlib.h>
void sign_off(void);
void too_bad(void);
int main(void)
{
	int n;atexit(sign_off); /* 注册 sign_off()函数 */
	puts("Enter an integer:"); 
	if (scanf("%d", &n) != 1) 
	{ 
		puts("That's no integer!"); 
		atexit(too_bad);/* 注册 too_bad()函数 */
		exit(EXIT_FAILURE); 
	}
	printf("%d is %s.\n", n, (n % 2 == 0) ? "even" : "odd");
	return 0; 
}
void sign_off(void)
{ 
	puts("Thus terminates another magnificent program from"); 
	puts("SeeSaw Software!"); 
}

void too_bad(void)
{
	puts("SeeSaw Software extends its heartfelt condolences"); 	
	puts("to you upon the failure of your program.");
}
/*下面是该程序的一个运行示例:
Enter an integer : 212
212 is even.
Thus terminates another magnificent program fromSeeSaw Software!
如果在IDE中运行,可能看不到最后两行。
下面是另一个运行示例:
Enter an integer : 
what ? 
That's no integer!
SeeSaw Software extends its heartfelt condolencesto you upon the failure of your program.
Thus terminates another magnificent program fromSeeSaw Software!
在IDE中运行,可能看不到最后4行。
接下来,我们讨论atexit()和exit()的参数。*/

在这里插入图片描述
即使没有显式调用exit(),还是会调用sign_off(),因为main()结束时会隐式调用exit()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扳手的海角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值