分享

exit(0),return 0有什么区别呢?

 fisher60 2011-09-16
exit好象在stdio.h里面,所以要有包含头文件
return是返回函数调用,如果返回的是main函数,则为退出程序
exit是在调用处强行退出程序,运行一次程序就结束
-------------------------------------------------------------------
return

是返回

函数返回
而exit是退出
-------------------------------------------------------------------
exit(1)表示异常退出.这个1是返回给操作系统的不过在DOS好像不需要这个返回值
exit(0)表示正常退出
-------------------------------------------------------------------
无论写在那里,都是程序推出,dos和windows中没有什么不一样,最多是系统处理的不一样。
数字0,1,-1会被写入环境变量ERRORLEVEL,其它程序可以由此判断程序结束状态。
一般0为正常推出,其它数字为异常,其对应的错误可以自己指定。
-------------------------------------------------------------------
返回给操作系统的,0是正常退出,其他值是异常退出,在退出前可以给出一些提示信息,或在调试程序中察看出错原因.
-------------------------------------------------------------------
Run-Time   Library   Reference      

exit,   _exit
Terminate   the   calling   process   after   cleanup   (exit)   or   immediately   (_exit).

void   exit(  
      int   status  
);
void   _exit(  
      int   status  
);
Parameter
status  
Exit   status.  
Remarks
The   exit   and   _exit   functions   terminate   the   calling   process.   exit   calls,   in   last-in-first-out   (LIFO)   order,   the   functions   registered   by   atexit   and   _onexit,   then   flushes   all   file   buffers   before   terminating   the   process.   _exit   terminates   the   process   without   processing   atexit   or   _onexit   or   flushing   stream   buffers.   The   status   value   is   typically   set   to   0   to   indicate   a   normal   exit   and   set   to   some   other   value   to   indicate   an   error.

Although   the   exit   and   _exit   calls   do   not   return   a   value,   the   low-order   byte   of   status   is   made   available   to   the   waiting   calling   process,   if   one   exists,   after   the   calling   process   exits.   The   status   value   is   available   to   the   operating-system   batch   command   ERRORLEVEL   and   is   represented   by   one   of   two   constants:   EXIT_SUCCESS,   which   represents   a   value   of   0,   or   EXIT_FAILURE,   which   represents   a   value   of   1.   The   behavior   of   exit,   _exit,   _cexit,   and   _c_exit   is   as   follows.

Function   Description  
exit   Performs   complete   C   library   termination   procedures,   terminates   the   process,   and   exits   with   the   supplied   status   code.  
_exit   Performs   quick   C   library   termination   procedures,   terminates   the   process,   and   exits   with   the   supplied   status   code.  
_cexit   Performs   complete   C   library   termination   procedures   and   returns   to   the   caller,   but   does   not   terminate   the   process.  
_c_exit   Performs   quick   C   library   termination   procedures   and   returns   to   the   caller,   but   does   not   terminate   the   process.  

When   you   call   the   exit   or   _exit   functions,   the   destructors   for   any   temporary   or   automatic   objects   that   exist   at   the   time   of   the   call   are   not   called.   An   automatic   object   is   an   object   that   is   defined   in   a   function   where   the   object   is   not   declared   to   be   static.   A   temporary   object   is   an   object   created   by   the   compiler.   To   destroy   an   automatic   object   before   calling   exit   or   _exit,   explicitly   call   the   destructor   for   the   object,   as   follows:  

myObject.myClass::~myClass();
You   should   not   call   exit   from   DllMain   with   DLL_PROCESS_ATTACH.   If   you   want   to   exit   the   DLLMain   function,   return   FALSE   from   DLL_PROCESS_ATTACH.

Requirements
Function   Required   header   Compatibility  
exit   <process.h>   or   <stdlib.h>   ANSI,   Win   98,   Win   Me,   Win   NT,   Win   2000,   Win   XP  
_exit   <process.h>   or   <stdlib.h>   Win   98,   Win   Me,   Win   NT,   Win   2000,   Win   XP  

For   additional   compatibility   information,   see   Compatibility   in   the   Introduction.

Libraries

All   versions   of   the   C   run-time   libraries.

Example
/*   EXITER.C:   This   program   prompts   the   user   for   a   yes
  *   or   no   and   returns   an   exit   code   of   1   if   the
  *   user   answers   Y   or   y;   otherwise   it   returns   0.   The
  *   error   code   could   be   tested   in   a   batch   file.
  */

#include   <conio.h>
#include   <stdlib.h>

void   main(   void   )
{
      int   ch;

      _cputs(   "Yes   or   no?   "   );
      ch   =   _getch();
      _cputs(   "\r\n "   );
      if(   toupper(   ch   )   ==   'Y '   )
            exit(   1   );
      else
            exit(   0   );
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多