【Linux】 |
您所在的位置:网站首页 › exit函数有什么用 › 【Linux】 |
功能:
_exit() :退出程序。exit(0):运行正常退出程序;exit(1):运行异常退出程序;return():返回函数,若在主函数中,则会退出函数并返回值。
_exit() 和 exit() 区别: exit()会将缓冲区的数据写完再结束进程到内核中去(退出进程会清理I/O缓冲区)。_exit() 直接结束进程进入到内核中。exit() 函数定义在 stdlib.h 中,_exit() 定义在 unistd.h 中。图示: 示例: _exit() #include #include int main(void) { printf("hello\n"); printf("OK"); _exit(0); }输出结果: helloexit() #include #include int main(void) { printf("hello\n"); printf("OK"); exit(0); }输出结果: hello OK原因: printf函数使用的是缓冲I/O的方式,该函数在遇到“\n“换行符时自动的从缓冲区中将记录读出。 exit() 将缓冲区的数据写完后才能退出来,所以调用exit()函数后程序并不会马上退出,会把OK也输出出来。 _exit() 是直接退出进入到内核中了。 exit(0) 和 exit(1) 区别: exit(0):运行正常退出程序;exit(1):运行异常退出程序,返回值1是返回给操作系统的; return 和 exit() 区别: return 是关键字;exit() 是函数。return 是语言级别的,表示调用堆栈的返回;而 exit() 是系统调用级别的,表示进程的结束。return 是退出(返回)函数,将控制权移交给递归的前一级;exit() 是直接退出进程。在最初调用的main函数中调用return和exit的现象很模糊,非主函数中调用return和exit效果很明显。
参考: https://blog.csdn.net/yyfwd/article/details/50548359 https://www.cnblogs.com/ECJTUACM-873284962/p/6882448.html#autoid-0-0-0 https://baike.so.com/doc/6759674-6974306.html https://blog.csdn.net/qq_23929673/article/details/99670478 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |