C ++中main的正确声明是什么?

C ++中main的正确声明是什么?

问题描述:

  • C ++中 main 函数的正确签名是什么?

  • What is the proper signature of the main function in C++?

正确的返回类型是什么,从 main 返回值是什么意思?

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?

这是特定于系统的吗?

这些规则是否随时间改变了?

Have those rules changed over time?

如果我违反了他们会发生什么?

What happens if I violate them?

必须在全局命名空间中将 main 函数声明为非成员函数.这意味着它不能是类的静态或非静态成员函数,也不能放置在名称空间(甚至是未命名的名称空间)中.

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).

在C ++中,名称 main 并不保留,只是作为全局名称空间中的函数使用.您可以自由声明其他名为 main 的实体,其中包括类,变量,枚举,成员函数以及不在全局命名空间中的非成员函数.

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.

您可以将名为 main 的函数声明为成员函数或在命名空间中,但是这样的函数不是指定程序开始位置的 main 函数.

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.

不能将 main 函数声明为 static inline .它也不能过载.全局名称空间中只能有一个名为 main 的函数.

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.

不能在程序中使用 main 函数:不允许在代码的任何位置调用 main 函数,也不允许使用其地址

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.

main 的返回类型必须为 int .不允许使用其他返回类型(此规则以粗体显示,因为通常会看到不正确的程序声明了 main 且返回类型为 void ;这可能是最常见的经常违反有关 main 函数的规则).

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).

必须允许两个 main 声明:

int main()               // (1)
int main(int, char*[])   // (2)

(1)中,没有参数.

(2)中,有两个参数,它们通常分别命名为 argc argv . argv 是指向表示程序参数的C字符串数组的指针. argc argv 数组中参数的数量.

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.

通常, argv [0] 包含程序的名称,但并非总是如此.保证 argv [argc] 是一个空指针.

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.

请注意,由于数组类型参数(例如 char * [] )实际上只是伪装的指针类型参数,因此以下两种方法都是编写(2),它们的含义完全相同:

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()将返回零以指示成功,而返回非零以指示失败.您无需在 main()中显式地编写 return 语句:如果让 main()返回而没有显式的 return语句,就如同您编写 return 0; 一样.以下两个 main()函数具有相同的行为:

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; }

< cstdlib> 中定义了两个宏, EXIT_SUCCESS EXIT_FAILURE ,也可以从 main()分别表示成功和失败.

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.

main()返回的值传递给 exit()函数,该函数终止程序.

The value returned by main() is passed to the exit() function, which terminates the program.

请注意,所有这些仅适用于针对托管环境(非正式地,您具有完整的标准库并且有一个操作系统在运行您的程序的环境)进行编译的情况.也有可能针对独立环境(例如某些类型的嵌入式系统)编译C ++程序,在这种情况下,启动和终止完全由实现定义,而 main()函数可能不是甚至是必需的.但是,如果您正在为现代台式机操作系统编写C ++,那么您是在为托管环境进行编译.

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.