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
=================================================================================================================
- What should main() return in C/C++?
- What does `return x ? : 1` mean in C language? [duplicate] stackoverflow
- what is #pragma in c
- What is “:-!!” in C code?
- What is Action in C#?
- C语言——int main,main 和 void main;return
- In C++, what is a memory arena?
- C语言中main函数return的总结
- c/c++中main方法中的return值 & 参数 & 剪切
- reloadData should be in main thread
- when should BOOL and bool be used in c
- main.c
- main.c
- main.c
- main.c
- main.c
- main.c
- main.c
- WEB ADI 修改列标题和增加LOV的SQL语句
- 坐标动态规划(tyvj p1124 花店橱窗)
- 欧拉回路
- Visual Studio 2010编译C++ 出现IntelliSense: 无法打开 源 文件 "stdafx.h"
- 被编译器给耍了
- What should main() return in C/C++?
- 个人虚拟主机选择应该考虑的七个问题
- 【动态规划】最大数列
- ORACLE 10G DATA GUARD 配置
- Linux-2.6.32.2内核在mini2440上的移植---添加ADC驱动
- error: collect2: ld returned 1 exit status 错误
- 一些数论题
- Eclipse 开源详细介绍
- rcp(插件开发)扩展点逻辑