C++ 中 main 的正确声明是什么?
问题
C++ 中
main
函数的正确签名是什么?正确的返回类型是什么?从
main
返回值意味着什么?允许的参数类型有哪些,它们的含义是什么?
这是特定于系统的吗?
这些规则随着时间的推移而改变吗?
如果我违反这些规定会怎样?
Questions
What is the proper signature of the
main
function in C++?What is the correct return type, and what does it mean to return a value from
main
?What are the allowed parameter types, and what are their meanings?
Is this system-specific?
Have those rules changed over time?
What happens if I violate them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
main
函数必须声明为全局命名空间中的非成员函数。这意味着它不能是类的静态或非静态成员函数,也不能放在命名空间中(甚至是未命名的命名空间)。除了作为全局命名空间中的函数之外,C++ 中不保留名称
main
。您可以自由声明名为main
的其他实体,其中包括类、变量、枚举、成员函数和不在全局命名空间中的非成员函数。您可以将名为
main
的函数声明为成员函数或在命名空间中,但这样的函数不会是指定程序启动位置的main
函数。main
函数不能声明为static
或inline
。也不能超载;全局命名空间中只能有一个名为main
的函数。main
函数不能在您的程序中使用:您不允许从代码中的任何位置调用main
函数,也不允许获取它的地址。main
的返回类型必须是int
。不允许其他返回类型(此规则以粗体显示,因为很常见地看到声明main
且返回类型为void
的错误程序;这可能是最常见的经常违反有关main
函数的规则)。必须允许有两个
main
声明:在 (1) 中,没有参数。
在(2)中,有两个参数,它们通常分别命名为
argc
和argv
。argv
是一个指向代表程序参数的 C 字符串数组的指针。argc
是argv
数组中的参数数量。通常,
argv[0]
包含程序的名称,但情况并非总是如此。argv[argc]
保证为空指针。请注意,由于数组类型参数(如
char*[]
)实际上只是变相的指针类型参数,因此以下两种都是编写 (2) 的有效方法它们的含义完全相同:某些实现可能允许其他类型和数量的参数;您必须检查实现的文档以了解它支持什么。
main()
预计返回零表示成功,返回非零表示失败。您不需要在main()
中显式编写return
语句:如果您让main()
返回而没有显式return
语句,与编写return 0;
相同。以下两个main()
函数具有相同的行为:有两个宏,
EXIT_SUCCESS
和EXIT_FAILURE
,在中定义;
也可以从main()
返回以分别指示成功和失败。main()
返回的值被传递给exit()
函数,该函数终止程序。请注意,所有这些仅适用于针对托管环境进行编译时(通俗地说,即拥有完整标准库并且有一个运行程序的操作系统的环境)。还可以为独立环境(例如,某些类型的嵌入式系统)编译 C++ 程序,在这种情况下,启动和终止完全由实现定义,并且
main()
函数可能不会甚至被要求。但是,如果您正在为现代桌面操作系统编写 C++,那么您正在为托管环境进行编译。The
main
function must be declared as a non-member function in the global namespace. This means that it cannot be a static or non-static member function of a class, nor can it be placed in a namespace (even the unnamed namespace).The name
main
is not reserved in C++ except as a function in the global namespace. You are free to declare other entities namedmain
, including among other things, classes, variables, enumerations, member functions, and non-member functions not in the global namespace.You can declare a function named
main
as a member function or in a namespace, but such a function would not be themain
function that designates where the program starts.The
main
function cannot be declared asstatic
orinline
. It also cannot be overloaded; there can be only one function namedmain
in the global namespace.The
main
function cannot be used in your program: you are not allowed to call themain
function from anywhere in your code, nor are you allowed to take its address.The return type of
main
must beint
. No other return type is allowed (this rule is in bold because it is very common to see incorrect programs that declaremain
with a return type ofvoid
; this is probably the most frequently violated rule concerning themain
function).There are two declarations of
main
that must be allowed:In (1), there are no parameters.
In (2), there are two parameters and they are conventionally named
argc
andargv
, respectively.argv
is a pointer to an array of C strings representing the arguments to the program.argc
is the number of arguments in theargv
array.Usually,
argv[0]
contains the name of the program, but this is not always the case.argv[argc]
is guaranteed to be a null pointer.Note that since an array type argument (like
char*[]
) is really just a pointer type argument in disguise, the following two are both valid ways to write (2) and they both mean exactly the same thing:Some implementations may allow other types and numbers of parameters; you'd have to check the documentation of your implementation to see what it supports.
main()
is expected to return zero to indicate success and non-zero to indicate failure. You are not required to explicitly write areturn
statement inmain()
: if you letmain()
return without an explicitreturn
statement, it's the same as if you had writtenreturn 0;
. The following twomain()
functions have the same behavior:There are two macros,
EXIT_SUCCESS
andEXIT_FAILURE
, defined in<cstdlib>
that can also be returned frommain()
to indicate success and failure, respectively.The value returned by
main()
is passed to theexit()
function, which terminates the program.Note that all of this applies only when compiling for a hosted environment (informally, an environment where you have a full standard library and there's an OS running your program). It is also possible to compile a C++ program for a freestanding environment (for example, some types of embedded systems), in which case startup and termination are wholly implementation-defined and a
main()
function may not even be required. If you're writing C++ for a modern desktop OS, though, you're compiling for a hosted environment.来自标准文档,3.6.1.2 主要功能,
int main() { / ... / }
和int main(int argc, char* argv[]) { / ... / }
希望有帮助..
From Standard docs., 3.6.1.2 Main Function,
int main() { / ... / }
andint main(int argc, char* argv[]) { / ... / }
Hope that helps..
最新发布的标准(C++14)的确切措辞是:
这清楚地表明,只要
main
的类型是int()
或int(int, char**)
类型,就允许使用替代拼写代码>.因此,以下内容也是允许的:int main(void)
auto main() -> int
int main ( )
signed int main()
typedef char **a; typedef int b, e; e main(bd, ac)
The exact wording of the latest published standard (C++14) is:
This makes it clear that alternative spellings are permitted so long as the type of
main
is the typeint()
orint(int, char**)
. So the following are also permitted:int main(void)
auto main() -> int
int main ( )
signed int main()
typedef char **a; typedef int b, e; e main(b d, a c)
两个有效的 main 是
int main()
和int main(int, char*[])
。其他任何东西可能会也可能不会编译。如果main
未显式返回值,则隐式返回 0。The two valid mains are
int main()
andint main(int, char*[])
. Any thing else may or may not compile. Ifmain
doesn't explicitly return a value, 0 is implicitly returned.有关返回值及其含义的详细信息
按照 3.6.1 (
[basic.start.main]
):std::exit
的行为,详细信息参见第 18.5 节 ([support.start.term]
),并描述了状态代码:Details on return values and their meaning
Per 3.6.1 (
[basic.start.main]
):The behavior of
std::exit
is detailed in section 18.5 ([support.start.term]
), and describes the status code: