C++ 中 main 的正确声明是什么? [英] What is the proper declaration of main in C++?

查看:45
本文介绍了C++ 中 main 的正确声明是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

名称 main 在 C++ 中不保留,除非作为全局命名空间中的函数.您可以自由声明名为 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 函数不能声明为 staticinline.它也不能过载;全局命名空间中只能有一个名为 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 有两个必须被允许的声明:

There are two declarations of main that must be allowed:

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

(1)中,没有参数.

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

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

有两个宏,EXIT_SUCCESSEXIT_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.

这篇关于C++ 中 main 的正确声明是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆