主要出口/返回 [英] main exit/return

查看:63
本文介绍了主要出口/返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我见过不同风格的代码,比如


main(argc,argv)

int argc;

char ** argv;

{

if(argc!= 2)exit(1);

退出(0);

}


int

main(argc,argv)

int argc;

char ** argv;

{

if(argc!= 2)return(-1) ;

否则返回(0);

}


我不确定是由哪个版本建议的标准和

理由避免采用其他方法。如果是main()函数,标准推荐什么是




提前致谢! ! !

解决方案

Rahul写道:


大家好,


我见过不同风格的代码,比如


main(argc,argv)

int argc;

char ** argv;



这有点古老。


{

if( argc!= 2)退出(1);

退出(0);

}


int

main(argc,argv)

int argc;

char ** argv;

{

如果(argc!= 2)返回(-1);

否则返回(0);

}


我''我不确定标准建议使用哪个版本,以及

避免采用其他方法的原因。在main()函数的情况下,标准建议

是什么?



头部:


int main(无效){...}

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

(和平凡的等效语法)


返回:


返回0;

返回EXIT_SUCCESS;


返回EXIT_FAILURE;


(有一个逃生舱,完全没有返回;避免。)


(`return`不需要括号;标准不是

表现出偏好,但我认为没有理由使用`()`当

时他们完全无关紧要,至少有一个原因 -

强调`return`不是函数调用 - 离开

out。)


相同的值适用于参数`exit`,但为什么当你在'main`时使用

`exit`?


-

原始和适度的计划必须不断扩展。/部门一般/


Hewlett-Pa ckard Limited注册号:

注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597英格兰


6月3日,10日:46 am,Chris Dollin< chris.dol ... @ hp.comwrote:


相同的值适用于`exit`的参数,但为什么要使用

`退出`当你在'main`时?



如果你使用atexit()来注册

函数会有一个细微的差别,当程序退出时会调用这些函数:如果你打电话退出

()来自主函数的主要局部变量仍将存在

这些函数执行时;如果你使用return语句那么本地

变量的主要信息都已消失。


6月3日上午11:55,Rahul< sam_ ... @ yahoo.co.inwrote:


大家好,


我见过不同风格的代码,比如


main(argc,argv)

int argc;

char ** argv;

{

if(argc!= 2)exit(1);

else exit(0);


}


int

main(argc,argv)

int argc;

char ** argv;

{

if(argc!= 2)return(-1);

else return(0);


}


我不确定标准建议使用哪个版本以及

避免采用其他方法的原因。在main()函数的情况下,标准建议

是什么?



C-FAQ的问题11.4。

< http://c-faq.com/>


Hi Everyone,

I have seen code in different styles like

main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) exit(1);
else exit(0);
}

int
main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) return(-1);
else return(0);
}

I''m not sure as to which version is advised by the standard and the
reason to avoid the other approach. What does the standard recommend
in case of main() function?

Thanks in advance ! ! !

解决方案

Rahul wrote:

Hi Everyone,

I have seen code in different styles like

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

That''s a bit ancient.

{
if(argc != 2) exit(1);
else exit(0);
}

int
main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) return(-1);
else return(0);
}

I''m not sure as to which version is advised by the standard and the
reason to avoid the other approach. What does the standard recommend
in case of main() function?

For the head:

int main(void) { ... }
int main( int argc, char **argv ) { ... }

(and trivial equivalent syntaxes)

For the return:

return 0;
return EXIT_SUCCESS;

return EXIT_FAILURE;

(there''s an escape-hatch for no return at all; avoid.)

(`return` doesn''t need parentheses; the standard doesn''t
exhibit a preference, but I see no reason to use `()` when
they''re completely irrelevant, and at least one reason -- to
emphasise that `return` is not a function call -- to leave
them out.)

The same values apply to the argument of `exit`, but why use
`exit` when you''re in `main`?

--
"The original and modest plans had to be continually extended."/Sector General/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


On Jun 3, 10:46 am, Chris Dollin <chris.dol...@hp.comwrote:

The same values apply to the argument of `exit`, but why use
`exit` when you''re in `main`?

There is a subtle difference if you used atexit () to register
functions that will be called when the program exits: If you call exit
() from main, local variables of the main function will still exist
when these functions execute; if you use a return statement then local
variables of main are gone.


On Jun 3, 11:55 am, Rahul <sam_...@yahoo.co.inwrote:

Hi Everyone,

I have seen code in different styles like

main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) exit(1);
else exit(0);

}

int
main(argc,argv)
int argc;
char **argv;
{
if(argc != 2) return(-1);
else return(0);

}

I''m not sure as to which version is advised by the standard and the
reason to avoid the other approach. What does the standard recommend
in case of main() function?

Question 11.4 of the C-FAQ.
<http://c-faq.com/>


这篇关于主要出口/返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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