What should main() return in C/C++?

来源:互联网 发布:彩虹岛网络传输异常 编辑:程序博客网 时间:2024/06/06 10:08

原帖: http://stackoverflow.com/questions/204476/what-should-main-return-in-c-c

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

What way is the most efficient and why? int main()? void main()? return 1? return 0?

===============================================================================================================================

the return value for main should indicate how the program exited. Normal exit is generally represented by a 0 return value from main. Abnormal termination is usually signalled by a non-zero return but there is no standard for how non-zero codes are interpreted. Also as noted by others, void main() is explicitly prohibited by the C++ standard and shouldn't be used. The valid C++ main signatures are:

int main()

and

int main(int argc, char* argv[])

which is equivalent to

int main(int argc, char** argv)

It's also worth noting that in C++, int main() can be left without a return value at which point it defaults to returning 0. This is also true with a C99 program. Whether return 0 should be omitted or not is open to debate. The range of valid C program main signatures is much greater.

Also, efficiency is not an issue with the main function. It can only be entered and left once (marking program start and termination) according to the C++ standard. For C, the case is different and re-entering main() is allowed, but should probably be avoided.

==============================================================================================================================

The accepted answer appears to be targetted for C++, so I thought I'd add an answer that pertains to C, and this differs in a few ways.

ISO/IEC 9899:1989 (C90):

main should be declared as either:

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

Or equivalent. For example, int main(int argc, char *argv[]) is equivalent to the second one. Further, the int return type can be omitted as it is a default.

If an implementation permits it, main can be declared in other ways, but this makes the program implementation defined, and no longer strictly conforming.

The standard defines 3 values for returning that are strictly conforming (that is, does not rely on implementation defined behaviour): 0 and EXIT_SUCCESS for a successful termination, and EXIT_FAILURE for an unsuccessful termination. Any other values are non-standard and implementation defined. main must have an explicit return statement at the end to avoid undefined behaviour.

Finally, there is nothing wrong from a standards point of view with calling main() from a program.

ISO/IEC 9899:1999 (C99):

For C99, everything is the same as above except:

  • The int return type may not be omitted.
  • You may omit the return statement from main. If you do, and main finished, there is an implicit return 0.

=================================================================================================================

I believe that main() should return either EXIT_SUCCESS or EXIT_FAILURE. They are defined in stdlib.h

=================================================================================================================

Return 0 on success and non-zero for error. This is the standard used by UNIX and DOS scripting to find out what happened with your program.

=================================================================================================================