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

发布于 2025-01-10 08:23:38 字数 269 浏览 5 评论 0原文

问题

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

汐鸠 2025-01-17 08:23:38

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

除了作为全局命名空间中的函数之外,C++ 中不保留名称 main。您可以自由声明名为 main 的其他实体,其中包括类、变量、枚举、成员函数和不在全局命名空间中的非成员函数。

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

main 函数不能声明为staticinline。也不能超载;全局命名空间中只能有一个名为 main 的函数。

main 函数不能在您的程序中使用:您不允许从代码中的任何位置调用 main 函数,也不允许获取它的地址。

main的返回类型必须是int。不允许其他返回类型(此规则以粗体显示,因为很常见地看到声明 main 且返回类型为 void 的错误程序;这可能是最常见的经常违反有关 main 函数的规则)。

必须允许有两个 main 声明:

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

(1) 中,没有参数。

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

通常,argv[0] 包含程序的名称,但情况并非总是如此。 argv[argc] 保证为空指针。

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

int main(int argc, char* argv[])
int main(int argc, char** argv)

某些实现可能允许其他类型和数量的参数;您必须检查实现的文档以了解它支持什么。

main() 预计返回零表示成功,返回非零表示失败。您不需要在 main() 中显式编写 return 语句:如果您让 main() 返回而没有显式 return 语句,与编写 return 0; 相同。以下两个 main() 函数具有相同的行为:

int main() { }
int main() { return 0; }

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

临风闻羌笛 2025-01-17 08:23:38

来自标准文档,3.6.1.2 主要功能

它应该有回报
int 类型的类型,但除此之外,其类型是实现定义的。
所有实现都应允许以下两种情况
主要定义:

int main() { / ... / }
int main(int argc, char* argv[]) { / ... / }

在后一种形式中,argc 应该是从环境中传递给程序的参数数量
程序运行。如果 argc 非零,这些参数应在 argv[0] 到 argv[argc-1] 中提供,作为指向
以 null 结尾的多字节字符串的初始字符.....

希望有帮助..

From Standard docs., 3.6.1.2 Main Function,

It shall have a return
type of type int, but otherwise its type is implementation-defined.
All implementations shall allow both of the following
definitions of main:

int main() { / ... / } and
int main(int argc, char* argv[]) { / ... / }

In the latter form argc shall be the number of arguments passed to the program from the environment in which the
program is run.If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as pointers to
the initial characters of null-terminated multibyte strings
.....

Hope that helps..

南汐寒笙箫 2025-01-17 08:23:38

最新发布的标准(C++14)的确切措辞是:

实现应允许两者

  • () 的函数返回 int

  • (int,指向 char) 的函数,返回 int

作为main的类型。

这清楚地表明,只要 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:

An implementation shall allow both

  • a function of () returning int and

  • a function of (int, pointer to pointer to char) returning int

as the type of main.

This makes it clear that alternative spellings are permitted so long as the type of main is the type int() or int(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)
雨轻弹 2025-01-17 08:23:38

两个有效的 main 是 int main()int main(int, char*[])。其他任何东西可能会也可能不会编译。如果 main 未显式返回值,则隐式返回 0

The two valid mains are int main() and int main(int, char*[]). Any thing else may or may not compile. If main doesn't explicitly return a value, 0 is implicitly returned.

天气好吗我好吗 2025-01-17 08:23:38

有关返回值及其含义的详细信息

按照 3.6.1 ([basic.start.main]):

main 中的 return 语句具有离开 main 函数(销毁具有自动存储持续时间的任何对象)并调用 std::exit 以返回值作为参数。如果控制到达 main 末尾而没有遇到 return 语句,则效果是执行


<前><代码>返回0;

std::exit 的行为,详细信息参见第 18.5 节 ([support.start.term]),并描述了状态代码:

最后,控制权返回到主机环境。如果状态为零或EXIT_SUCCESS,则返回状态成功终止的实现定义形式。如果状态为 EXIT_FAILURE,则返回未成功终止状态的实现定义形式。否则返回的状态是实现定义的。

Details on return values and their meaning

Per 3.6.1 ([basic.start.main]):

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;

The behavior of std::exit is detailed in section 18.5 ([support.start.term]), and describes the status code:

Finally, control is returned to the host environment. If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文