exit和return

来源:互联网 发布:威露士滴露哪个好 知乎 编辑:程序博客网 时间:2024/05/17 23:38
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 are called.
  • All C streams (open with functions in <cstdio>) are closed (and flushed, if buffered), and all files created with tmpfile are removed.
  • 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(),它也将终止程序。


原创粉丝点击