exit() 函数定义在 stdlib.h 头文件中。它帮助程序执行结束后返回操作系统。实际上,它从当前进程返回,而不执行其余代码。
void exit(int status); #where status indicates status value
exit() 函数接受一个参数。这里的状态码表示错误消息。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| 状态 | 返回给父进程的状态值 | 必需 |
exit() 函数不返回任何值。如果状态参数为 "0",则程序退出,不带任何错误消息。如果给定其他代码作为状态码,则表示系统可以处理错误消息。
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Program is Started....\n");
printf("Program is Exiting....\n");
exit(0);
printf("Program reaches to End....\n");
return(0);
}
输出
Program is Started.... Program reaches to End....
#include <stdio.h>
#include <stdlib.h>
int main (){
FILE *p = fopen("mydata.txt","r");
if (p == NULL) {
fprintf(stderr, "Opening file mydata.txt have error in function main()\n");
exit( EXIT_FAILURE );
}
fclose(p);
printf("Normal Return\n");
return EXIT_SUCCESS ;
}
输出
Opening file mydata.txt have error in function main()