exit 终止一个进程 atexit 注册终止函数

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值