Terminate calling process

Terminates the process normally, performing the regular cleanup for terminating programs.



Normal program termination performs the following (in the same order):


  • Objects associated with the current thread with thread storage duration are destroyed (C++11 only).
  • Objects with static storage duration are destroyed (C++) and functions registered with ​​atexit​
  • All C streams (open with functions in ​​<cstdio>​​​) are closed (and flushed, if buffered), and all files created with ​​tmpfile​
  • Control is returned to the host environment.


Note that objects with automatic storage are not destroyed by calling 

exit (C++).



If status is zero or  ​​EXIT_SUCCESS​​, a 

successful termination status is returned to the host environment.


If status is  ​​EXIT_FAILURE​​, an 

unsuccessful termination status is returned to the host environment.


Otherwise, the status returned depends on the system and library implementation.



exit()和return的区别:


按照ANSI C,在最初调用的main()中使用return和exit()的效果相同。


但要注意这里所说的是“最初调用”。如果main()在一个递归程序中,exit()仍然会终止程序;但return将


控制权移交给递归的前一级,直到最初的那一级,此时return才会终止程序。return和exit()的另一个区别


在于,即使在除main()之外的函数中调用exit(),它也将终止程序。