免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2117 | 回复: 5
打印 上一主题 下一主题

请教关于main返回值的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-07-11 13:24 |只看该作者 |倒序浏览
一般main程序最后都有return 0,而实际上不写这条语句程序也能正常执行,请问这条语句有什么用呢?
     另外,main的声明,当不需要接收参数时,int main(void)与main()有区别吗?还是只是习惯上的问题?     
       0和'\0'有区别吗?

论坛徽章:
0
2 [报告]
发表于 2003-07-11 14:37 |只看该作者

请教关于main返回值的问题

main必须被声明为int main(),因为在C的启动代码里缺省的是这样认为的,也就是说C的启动代码里为此做了工作(当然不同的机器是不同的,有些可能不需要做什么)。因此,如果你声明的main不是返回int,那么有可能会产生问题。
至于main(),是老的C版本的用法。老C对没有声明返回类型的函数默认为返回int。ANSI C里这是非法的。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2003-07-11 16:08 |只看该作者

请教关于main返回值的问题

main可以申明为
void main()
void main(void)
void main(int argc,char **argv)
只不过这样的申明返回给调用者的是随机数值。

main如何启动和处理参数的好像以前讨论过。

论坛徽章:
0
4 [报告]
发表于 2003-07-11 17:00 |只看该作者

请教关于main返回值的问题

不想写了,从C FAQ里拷一段自己看吧。
11.12a:        What's the correct declaration of main()?

A:        Either int main(), int main(void), or int main(int argc,
        char *argv[]) (with alternate spellings of argc and *argv[]
        obviously allowed).  See also questions 11.12b to 11.15 below.

        References: ISO Sec. 5.1.2.2.1, Sec. G.5.1; H&S Sec. 20.1 p.
        416; CT& Sec. 3.10 pp. 50-51.

11.12b:        Can I declare main() as void, to shut off these annoying
        "main returns no value" messages?

A:        No.  main() must be declared as returning an int, and as
        taking either zero or two arguments, of the appropriate types.
        If you're calling exit() but still getting warnings, you may
        have to insert a redundant return statement (or use some kind
        of "not reached" directive, if available).

        Declaring a function as void does not merely shut off or
        rearrange warnings: it may also result in a different function
        call/return sequence, incompatible with what the caller (in
        main's case, the C run-time startup code) expects.

        (Note that this discussion of main() pertains only to "hosted"
        implementations; none of it applies to "freestanding"
        implementations, which may not even have main().  However,
        freestanding implementations are comparatively rare, and if
        you're using one, you probably know it.  If you've never heard
        of the distinction, you're probably using a hosted
        implementation, and the above rules apply.)

        References: ISO Sec. 5.1.2.2.1, Sec. G.5.1; H&S Sec. 20.1 p.
        416; CT& Sec. 3.10 pp. 50-51.

11.13:        But what about main's third argument, envp?

A:        It's a non-standard (though common) extension.  If you really
        need to access the environment in ways beyond what the standard
        getenv() function provides, though, the global variable environ
        is probably a better avenue (though it's equally non-standard).

        References: ISO Sec. G.5.1; H&S Sec. 20.1 pp. 416-7.

11.14:        I believe that declaring void main() can't fail, since I'm
        calling exit() instead of returning, and anyway my operating
        system ignores a program's exit/return status.

A:        It doesn't matter whether main() returns or not, or whether
        anyone looks at the status; the problem is that when main() is
        misdeclared, its caller (the runtime startup code) may not even
        be able to *call* it correctly (due to the potential clash of
        calling conventions; see question 11.12b).

        It has been reported that programs using void main() and
        compiled using BC++ 4.5 can crash.  Some compilers (including
        DEC C V4.1 and gcc with certain warnings enabled) will complain
        about void main().

        Your operating system may ignore the exit status, and
        void main() may work for you, but it is not portable and not
        correct.

11.15:        The book I've been using, _C Programing for the Compleat Idiot_,
        always uses void main().

A:        Perhaps its author counts himself among the target audience.
        Many books unaccountably use void main() in examples, and assert
        that it's correct.  They're wrong.

论坛徽章:
0
5 [报告]
发表于 2003-07-11 17:12 |只看该作者

请教关于main返回值的问题

thanks!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
6 [报告]
发表于 2003-07-11 18:28 |只看该作者

请教关于main返回值的问题

哦,这个FAQ早就看过。ANSI C不建议使用void main的使用方式。
实际的编程中,也很少有人这么用。但是使用void main不能算是错误(当然有些编译器认为是错误)。

我之所以继续跟帖子的缘故便是希望大家不要断定什么所谓的FAQ。FAQ可以作为参考,但是不能当作圣经。

或许最好的办法是自己测试编译程序和main装载例程是怎么工作的。最权威的资料可能来此语某个特定编译程序的说明书。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP