什么是正确的主体声明? [英] What is the proper declaration of main?
问题描述
C ++中 main
函数的正确签名是什么?什么是正确的返回类型,从 main
返回值是什么意思?什么是允许的参数类型,它们的含义是什么?
这是系统特定的吗?这些规则是否随着时间而改变?
main
函数必须在全局命名空间中声明为非成员函数。这意味着它不能是类的静态或非静态成员函数,也不能放在命名空间(即使是未命名的命名空间)中。
除非作为全局命名空间中的函数,否则C ++中不保留名称 main
。您可以自由地声明名为 main
的其他实体,其中包括不在全局命名空间中的类,变量,枚举,成员函数和非成员函数。 p>
您可以声明一个名为 main
的函数作为成员函数或命名空间,但这样的函数不会是 main
函数指定程序启动的位置。
main
函数不能声明为 static
或 inline
。它也不能重载;在全局命名空间中只能有一个名为 main
的函数。
main
函数不能在你的程序中使用:你不允许从代码中的任何地方调用 main
函数,也不允许使用它的地址
main
的返回类型必须为 int
。不允许使用其他返回类型(此规则以粗体显示,因为看到不正确的程序声明 main
且返回类型为 void
;这可能是关于 main
函数的最常被违反的规则。
是必须允许的 main
的两个声明:
int main )//(1)
int main(int,char * [])//(2)
$ b b
在(1)中,没有参数。
在(2)中,有两个参数,它们通常被命名为 argc
和 argv
。 argv
是一个指向表示程序参数的C字符串数组的指针。 argc
是 argv
数组中的参数数。
通常, argv [0]
包含程序的名称,但并不总是这样。 argv [argc]
确保为空指针。
注意,由于数组类型参数code> char * [] )实际上只是伪装的指针类型参数,以下两种都是写入(2)的有效方式,完全一样的东西:
int main(int argc,char * argv [])
int main ,char ** argv)
一些实现可能允许其他类型和数量的参数;您必须检查实施的文档才能看到它的支持。
main()
返回零表示成功,非零表示失败。您不需要在 main()
中明确写入 return
语句:如果让 main()
返回没有明确的返回
语句,就像你写了 return 0;
。以下两个 main()
函数具有相同的行为:
int main(){}
int main(){return 0; }
有两个宏: EXIT_SUCCESS
< cstdlib>
中定义的 EXIT_FAILURE
,也可以从 / code>分别表示成功和失败。
main()
返回的值是传递到 exit()
函数,它会终止程序。
请注意,对于托管环境(非正式,一个环境,其中有一个完整的标准库,并有一个操作系统运行您的程序)。还可以为独立的环境(例如,某些类型的嵌入式系统)编译C ++程序,在这种情况下,启动和终止完全由实现定义,并且 main()
函数甚至可能不需要。如果你正在编写一个现代桌面操作系统的C ++,你正在编译一个托管环境。
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?
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 named main
, 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 the main
function that designates where the program starts.
The main
function cannot be declared as static
or inline
. It also cannot be overloaded; there can be only one function named main
in the global namespace.
The main
function cannot be used in your program: you are not allowed to call the main
function from anywhere in your code, nor are you allowed to take its address.
The return type of main
must be int
. No other return type is allowed (this rule is in bold because it is very common to see incorrect programs that declare main
with a return type of void
; this is probably the most frequently violated rule concerning the main
function).
There are two declarations of main
that must be allowed:
int main() // (1)
int main(int, char*[]) // (2)
In (1), there are no parameters.
In (2), there are two parameters and they are conventionally named argc
and argv
, respectively. argv
is a pointer to an array of C strings representing the arguments to the program. argc
is the number of arguments in the argv
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:
int main(int argc, char* argv[])
int main(int argc, char** argv)
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 a return
statement in main()
: if you let main()
return without an explicit return
statement, it's the same as if you had written return 0;
. The following two main()
functions have the same behavior:
int main() { }
int main() { return 0; }
There are two macros, EXIT_SUCCESS
and EXIT_FAILURE
, defined in <cstdlib>
that can also be returned from main()
to indicate success and failure, respectively.
The value returned by main()
is passed to the exit()
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.
这篇关于什么是正确的主体声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!