退货和退货之间的区别 [英] Difference between return and exit

查看:88
本文介绍了退货和退货之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我想知道,

main()函数中退出和返回的区别是什么?对我来说,他们看起来都一样,或者不是吗?如果

他们不是,我应该在哪种情况下使用?


我也想知道合并标准是否合情合理br />
状态返回。

exp:


int main(){

printf(" Hello World \ n");

返回EXIT_SUCCESS;

}


Greetz,


Noud Aldenhoven

Hello,

I was wondering, what''s the difference between exit and return in the
main() function? For me they both look the same, or aren''t they? And if
they aren''t, which should I use in which situation?

Also I was wondering if it whould be wise to combine the standard
status with return.
exp:

int main(){
printf("Hello World\n");
return EXIT_SUCCESS;
}

Greetz,

Noud Aldenhoven

推荐答案

jwaixs< jw **** @ gmail.com>写道:
jwaixs <jw****@gmail.com> wrote:
我想知道,
main()函数中退出和返回的区别是什么?对我来说,他们看起来都一样,或者不是吗?如果他们不是,我应该在哪种情况下使用?
I was wondering, what''s the difference between exit and return in the
main() function? For me they both look the same, or aren''t they? And if
they aren''t, which should I use in which situation?




FAQ 11.16。


-

Stan Tobias

mailx`echo si *** @ FamOuS.BedBuG.pAlS.INVA LID | sed s / [[:upper:]] // g`



FAQ 11.16.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`


jwaixs写道:
你好,

我想知道,
main()函数中退出和返回的区别是什么?对我来说,他们看起来都一样,或者不是吗?如果他们不是,我应该在哪种情况下使用?


它们几乎相同。据我所知

只有两个区别:


- main()函数可以递归调用,如

任何其他C函数。如果是,只有从

返回的第一个呼叫终止该程序;他们的程序

只是在后续调用返回时继续运行。

当然,exit()将终止程序,无论如何。

这里' '是一个愚蠢的程序,以相反的顺序打印其命令行

参数:


#include< stdio.h>

int main(int argc,char ** argv){

if(argc> 0){

main(argc - 1,argv + 1);

put(* argv);

}

返回0;

}


很明显,如果使用exit()而不是返回,这个程序的行为将完全不同。


- 当main()时返回,其所有局部变量停止

存在。如果程序总结活动试图引用

这些变量(通过之前存储的指针),那么

可能会有麻烦。用

atexit()注册的函数在这里是一个潜在的麻烦来源;

setbuf()和setvbuf()如果他们使用'auto''缓冲区和

他们的流尚未关闭。调用exit(),

虽然保持调用者的变量不变。


就个人而言,我更喜欢从main返回而不是调用

退出(),但这是一个弱的偏好。

我也想知道将标准
状态与返回结合起来是否明智。
exp:

int main(){
printf(" Hello World \ n");
返回EXIT_SUCCESS;
}
Hello,

I was wondering, what''s the difference between exit and return in the
main() function? For me they both look the same, or aren''t they? And if
they aren''t, which should I use in which situation?
They are almost equivalent. As far as I know there
are only two differences:

- The main() function can be called recursively, like
any other C function. If it is, only the return from
the first call terminates the program; they program
just keeps on running when a subsequent call returns.
Of course, exit() will terminate the program regardless.
Here''s a silly program that prints its command-line
arguments in reverse order:

#include <stdio.h>
int main(int argc, char **argv) {
if (argc > 0) {
main(argc - 1, argv + 1);
puts (*argv);
}
return 0;
}

Clearly, this program''s behavior would be completely
different if exit() were used instead of return.

- When main() returns, all its local variables cease to
exist. If program wrap-up activities try to refer to
these variables (via pointers stored earlier), there
will probably be trouble. Functions registered with
atexit() are a potential source of trouble here; so are
setbuf() and setvbuf() if they use `auto'' buffers and
their streams have not yet been closed. Calling exit(),
though, leaves the caller''s variables intact.

Personally, I prefer to return from main rather than call
exit(), but it''s a weak preference.
Also I was wondering if it whould be wise to combine the standard
status with return.
exp:

int main(){
printf("Hello World\n");
return EXIT_SUCCESS;
}




你应该总是从main()返回某种值或者

将某种值传递给exit()。三个便携式值

是0,EXIT_SUCCESS和EXIT_FAILURE;你的系统可能还有b $ b识别其他价值。


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子


jwaixs在24/07/05写道:
jwaixs wrote on 24/07/05 :
我想知道,
main()函数中退出和返回的区别是什么?


没有明显区别。 exit()在main()中没用。

对我来说,他们看起来都一样,或者不是吗?如果
他们不是,我应该在哪种情况下使用?

我也想知道将标准状态与回归结合起来是否明智。


是的,它会是。

int main(){
printf(" Hello World \ nn);;
返回EXIT_SUCCESS;
}
I was wondering, what''s the difference between exit and return in the
main() function?
No visible difference. exit() is useless in main().
For me they both look the same, or aren''t they? And if
they aren''t, which should I use in which situation?

Also I was wondering if it whould be wise to combine the standard
status with return.
Yes, it would be.
int main(){
printf("Hello World\n");
return EXIT_SUCCESS;
}




假设您已包含< stdio.h>和< stdlib.h> ;,这里没有什么

错了。


-

Emmanuel

C-FAQ: http:// www .eskimo.com / ~scs / C-faq / faq.html

C库: http://www.dinkumware.com/refxc.html


有10种类型的人在今天的世界;

那些了解二元的人,以及那些不懂的人。



Assuming you have included <stdio.h> and <stdlib.h>, there is nothing
wrong here.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."


这篇关于退货和退货之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆