exit 终止程序
void exit(int status)
原型位于 stdlib.h, process.h
适用于 UNIX 系统,在 ANSI C 中有定义
eixt 终止调用进程, 在退出程序之前,关闭所有文件, 缓冲输出内容将刷新定义,并调用所有已刷新的”出口函数“(由 atexit 定义)。
参数 status 被用来提供调用进程的出口状态,一般来说, 0 表示正常出口, 非 0 值表示有错误发生。
此时 status 将置为 下列值之一
EXIT_SUCCESS 正常终止程序
EXIT_FAILURE 非正常终止程序,并通知操作系统程序有错
没有返回值
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
int status;
printf(" Enter either 1 or 2/n");
status = getch();
/* Set DOS errorlevel, 不太懂哎 :( */
exit(status - '0');
/* Note: this line is never reached */
printf(" You'll never see this/n");
return 0;
}
atexit
功 能: 注册终止函数(即main执行结束后调用的函数)
用 法: int atexit(atexit_t func);
注意:atexit()注册的函数类型应为不接受任何参数的void函数,
exit调用这些注册函数的顺序与它们 登记时候的顺序相反。
与 ANSI C 兼容
#include<stdio.h>
#include<stdlib.h>
void exit_fn1(void)
{
printf("Exit function #1 called/n");
}
void exit_fn2(void)
{
printf("Exit function #2 called/n");
}
int main(void)
{
/* 注册 exit_fn1 。Post exit function #1 */
atexit(exit_fn1);
/* 注册 exit_fn2 。 Post exit function #2 */
atexit(exit_fn2);
return 0 ;
}
return 0 ; 改成 exit(0) 时,输出相同
return 0 ; 改成 _exit(0) 时, 什么也不输出, 因为它不调用 出口函数
return 0 ; 改成 abort() 时, 只输出 Abnormal program termination