#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()