I am working on C-brain teasers: write standard hello-world programs, without semi-colon.
My best answer is:
int main {zero} {if (printf ("hello world! \ N"), exit (0) 0) {/ * I am nothing} I do not understand why I could not get Compiler Error: > error C4716: return the value to 'main':
I have tried to declare other actions a return-type, but missed the return statement, And this compiler error Do Rapt.
int foo (zero) {if (printf ("hello world! \ N"), exit (0), true) {/ * Nothing}}} int main (zero) {foo (); }
and do not get a compiler error on foo. If I remove "exit (0)", then I'm getting the do compiler error. Apparently the compiler has the knowledge that "exit" is a special task? It seems very strange to me.
As Jens pointed out in a comment, the post code is not displayed undefined behavior original answer right here And does not really answer this question (still reading everything after a few years).
Can not be expressed as a question, "Why not for MSVC issue alert C4716 main ()
in the same situation that would be for other tasks"?
Note that the diagnostic C4716 is a warning, there is no error as far as the Si language is concerned (anyway from a standard point), there is no need to diagnose a non-error . But it does not really explain why there is a difference, it's just a technical, which means that you can not complain too much ...
Why the warning for MSVC was not released Explanation main ()
When it does for other functions, in fact, only the MSVC team can be answered by anyone, as far as I can tell, docs do not understand the difference, but maybe I have some Remember Received; So all I can guess is:
In C ++, the main ()
function is specially treated in that it has a built-in back 0 ;
Closing Brace
I suspect that Microsoft's C compiler provides the same treatment when it is compiled in C mode (if you see the assembly code, then clear the EX register Even if no return 0; ), so where the compiler is concerned, there is no reason to issue the warning C4716. Note that C90 is analogous to C90 in Microsoft's C mode, C99 does not correspond in C90 with "undefined behavior" at the end of
main
, although always returning to meet the low requirements of undefined behavior That's why there is no problem.
Even if the program did end in the query main ()
(which resulted in undefined behavior) is going on, it will not be so. .
Basic, not so good answer:
In ANSI / ISO 90C, this is an undefined behavior, so MS will actually generate an error (but They are not required by the standard). In the C99, at the end of the standard (main) gives a valid code return
- as is C ++.
So if it is compiled in C ++ or CML, then there is no error and it is same as Interesting (well, not), many compilers (VC9, VC6, GCC 3.4.5, Digital Mars, Camou) I tried to set it with my basic, mostly default options (The environment always uses too much for quick-n-coded testing of code snippets) The only compiler that warns about random return statements when compiling as a C ++ program (VC6) For the Do not complain while doing the calculations. The function is not named as 0 back; <0> CO 090 results in undefined behavior (which does not require diagnosis).
main
, most compilers complain (a warning or error) Digital Mars compiles for C and GCC C or C + + Does not have to.
Comments
Post a Comment