为什么main()的结尾没有返回编译器错误? [英] Why no compiler error for main() without a return at the end?

查看:150
本文介绍了为什么main()的结尾没有返回编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在C-脑筋急转弯:写标准的Hello-World程序,没有分号

I'm working on a C-brain teaser: Write the standard Hello-World program, without semi-colons.

我的最好的答案迄今:

int main(void)
{
    if (printf("Hello World!\n"), exit(0), 0)
    {
        /* do nothing */
    }
}

但我不明白为什么我没有得到编译器错误(Visual Studio中):

But I don't understand why I don't get compiler error (Visual Studio):

error C4716: 'main' : must return a value

我试着声明的返回类型等功能,但缺少一个返回语句,并获得此编译器错误。

I've tried other functions with a return-type declared, but missing a return-statement, and get this compiler error.

请注意,我也尝试:

int foo(void)
{
    if (printf("Hello World!\n"), exit(0), true)
    {
        /* do nothing */
    }
}

int main(void)
{
    foo();
}

和不上富得到一个编译错误。如果我删除了出口(0),我的让编译器错误。显然,编译器有知识的退出是一个特殊的功能?这似乎很奇怪我。

And don't get a compiler error on foo. If I remove the "exit(0)", I do get the compiler error. Apparently the compiler has knowledge that "exit" is a special function? This seems very odd to me.

推荐答案

作为延在评论中指出的那样,公布code没有表现出不确定的行为。原来这里的答案是不正确的甚至不似乎真的反正来回答这个问题(在重新阅读一切几年后)。

As Jens pointed out in a comment, the posted code does not exhibit undefined behavior. The original answer here isn't correct and doesn't even really seem to answer the question anyway (on re-reading everything a few years later).

问题可以概括为,为什么不MSVC发出警告的 C4716的main()它会为其它功能相同的情况下?

The question can be summed up as, "why doesn't MSVC issue warning C4716 for main() in the same circumstances it would for other functions"?

请注意,诊断C4716是一个警告,而不是错误。至于C语言而言(从视图反正一个标准点),有从未来诊断一个非错​​误的一个要求。但是这并没有真正解释为什么有区别,它只是意味着你不能抱怨一个技术性太多...

Note that diagnostic C4716 is a warning, not an error. As far as the C language is concerned (from a standards point of view anyway), there's never a requirement to diagnose a non-error. but that doesn't really explain why there's a difference, it's just a technicality that may mean you can't complain too much...

为什么MSVC不发出警告的真正解释的main()当它用于其它功能可以真正只能由有人在队MSVC回答。据我所知道的,文档不解释这种差异,但也许我错过了什么;因此,所有我能做的就是猜测:

The real explanation for why MSVC doesn't issue the warning for main() when it does for other functions can really only be answered by someone on the MSVC team. As far as I can tell, the docs do not explain the difference, but maybe I missed something; so all I can do is speculate:

在C ++中,的main()功能是在经过特殊处理的一个隐含的返回0有; 之前右大括号。

In C++, the main() function is treated specially in that there's an implicit return 0; just before the closing brace.

我怀疑微软的C编译器提供,当它在C模式下的编译(如果你看一下组装code,如果有EAX寄存器甚至清除了同样的待遇没有返回0; ),因此只要编译器而言,没有理由发出警告C4716。需要注意的是微软的C模式是符合C90,C99不兼容。在C90'逃跑'的端部的的main()是未定义行为。然而,总是返回0满足未定义行为的要求低,所以没有问题。

I suspect that Microsoft's C compiler provides the same treatment when it's compiling in C mode (if you look at the assembly code, the EAX register is cleared even if there's no return 0;), therefore as far as the compiler is concerned there is no reason to issue warning C4716. Note that Microsoft's C mode is C90 compliant, not C99 compliant. In C90 'running off the end' of main() has undefined behavior. However, always returning 0 meets the low requirements of undefined behavior, so there's no problem.

所以,即使在有问题的程序的没有的流失结束的main()(导致不确定的行为)仍然不会是一个警告。

So even if the program in the question did run off the end main() (resulting in undefined behavior) there still wouldn't be a warning.

原来,不太好回答:

<击>在ANSI / ISO 90℃,这是不确定的行为,所以MS真的应该产生一个错误(但不是由标准的要求)。在C99标准允许一个隐含的收益在main()结束 - 一样C ++

In ANSI/ISO 90 C, this is undefined behavior, so MS really should produce an error (but they aren't required to by the standard). In C99 the standard permits an implied return at the end of main() - as does C++.

因此​​,如果这被编译为C ++或C99,没有错误,这是一样的返回0; 。 C90结果不确定的行为(不要求诊断)。

So if this is compiled as C++ or C99, there's no error and it's the same as return 0;. C90 results in undefined behavior (which does not require a diagnostic).

有趣的(好吧,也许不是),几个编译器(VC9,VC6,GCC 3.4.5,数字火星,科莫)我设置我的基本的,主要是默认选项尝试这对(环境我pretty多少总是使用code段的快速正肮脏的测试),警告有关失踪return语句仅有的编译器作为C ++程序编译(编译用于C时VC6不抱怨)时为VC6。

Interestingly (well, maybe not), of the several compilers (VC9, VC6, GCC 3.4.5, Digital Mars, Comeau) I tried this on with my basic, mostly default options set (the environment I pretty much always use for quick-n-dirty testing of code snippets) the only compiler that warns about the missing return statement is VC6 when compiling as a C++ program (VC6 does not complain when compiling for C).

大多数编译器的抱怨(警告或错误),如果函数没有命名。数字火星编译用于C时,不和GCC没有为C或C ++。

Most of the compilers complain (a warning or error) if the function is not named main. Digital Mars when compiling for C does not and GCC doesn't for C or C++.

这篇关于为什么main()的结尾没有返回编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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