解释下exit()函数,还有exit()函数和 return()的区别??

momo2chao 2009-04-10 09:11:44
麻烦详细解释下exit()函数
还有exit()函数和 return()的区别??
最好给我个小的实例,谢谢。。。
...全文
109 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
麻烦详细解释下exit()函数,是不是调用exit()函数要包含什么头文件啊?vc中。。
还有exit()函数和 return()的区别??

exit()是退出当前进程.
而return是返回(退出)当前函数.

具体的你自己google咯,概念这东西,网上很详细的.
Bible_Chou 2009-04-10
  • 打赏
  • 举报
回复
1. void exit( int status );
Performs complete C library termination procedures, terminates the process, and exits with the supplied status code.

2. return
jump-statement :
return expression opt ;
The return statement terminates the execution of a function and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can also return a value to the calling function.

mengde007 2009-04-10
  • 打赏
  • 举报
回复
int f3() {
return 3;
}

int f2() {
return f3() + 1;
}

int f1() {
return f2() + 2;
}

int main () {
return f1();
}

你把程序里的 return 挨个用 exit 替换,然后在 Linux / Unix系统下面试试看。每次执行完毕都用 echo $? 看看退出值。

exit 是用来结束一个程序的执行的,而 return 只是用来从一个函数中返回。
ForestDB 2009-04-10
  • 打赏
  • 举报
回复
简单的讲,exit结束当前的进程,return返回到调用者。
mengde007 2009-04-10
  • 打赏
  • 举报
回复
exit() 结束当前进程/当前程序/,在整个程序中,只要调用 exit ,就结束
return() 是当前函数返回,当然如果是在主函数main, 自然也就结束当前进程了,如果不是,那就是退回上一层调用

exit(0) 是非正常退出
exit(1) 是正常退出
Dinelgua 2009-04-10
  • 打赏
  • 举报
回复
汇编代码对比如下,exit 调用 @ILT+1275(_exit) (411500h)
就退出了

int func(void)
{
00411C00 push ebp
00411C01 mov ebp,esp
00411C03 sub esp,0C0h
00411C09 push ebx
00411C0A push esi
00411C0B push edi
00411C0C lea edi,[ebp-0C0h]
00411C12 mov ecx,30h
00411C17 mov eax,0CCCCCCCCh
00411C1C rep stos dword ptr [edi]
//exit(0);
return 1;
00411C1E mov eax,1
}



int func(void)
{
00411C00 push ebp
00411C01 mov ebp,esp
00411C03 sub esp,0C0h
00411C09 push ebx
00411C0A push esi
00411C0B push edi
00411C0C lea edi,[ebp-0C0h]
00411C12 mov ecx,30h
00411C17 mov eax,0CCCCCCCCh
00411C1C rep stos dword ptr [edi]
exit(0);
00411C1E push 0
00411C20 call @ILT+1275(_exit) (411500h)
return 1;
}
Hayden_yang 2009-04-10
  • 打赏
  • 举报
回复
exit(0) 表示程式正常, exit(1)/exit(-1)表示程式异常退出
exit() 结束当前进程/当前程式/,在整个程式中,只要调用 exit ,就结束
return() 是当前函数返回,当然如果是在主函数main, 自然也就结束当前进程了,如果不是,那就是退回上一层调用。在多个进程时.如果有时要检测上进程是否正常退出的.就要用到上个进程的返回值..
exit(1)表示进程正常退出. 返回 1;
exit(0)表示进程非正常退出. 返回 0.
Dinelgua 2009-04-10
  • 打赏
  • 举报
回复
exit直接退出程序,后面不再执行
return只是 所在函数返回,后面会继续执行



int func(void)
{
//exit(0);
return 1;
}
void main()
{
func();

}

mengde007 2009-04-10
  • 打赏
  • 举报
回复
exit()强制退出,return;正常退出。

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧