C语言 C 中 main(void) 和 main() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3711048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:26:25  来源:igfitidea点击:

Difference between main(void) and main() in C

cmain

提问by Archit

Can anyone tell me the difference between int main()and int main(void)? Why do both of them work and what is the default argument to int main()?

谁能告诉我之间的差异int main()int main(void)?为什么它们都可以工作,默认参数是int main()什么?

回答by bmargulies

No difference under ordinary circumstances. This is no 'default argument to main()', since it has no arguments at all.

一般情况下没有区别。这不是“main() 的默认参数”,因为它根本没有参数。

Here's the un-ordinary circumstance. If you write your own call to main, then ()will permit you to pass it any parameters you like, while (void)will force you to pass it none. Still, none of this matters in terms of the 99.99999999% case, which is main being invoked by the runtime to launch your program. The runtime neither knows nor cares if you write ()or (void).

这是不寻常的情况。如果您编写自己对 main 的调用,()则将允许您向它传递任何您喜欢的参数,而(void)将强制您不传递任何参数。尽管如此,就 99.99999999% 的情况而言,这些都无关紧要,运行时主要调用它来启动您的程序。运行时既不知道也不关心您是否编写()(void)

If you code the standard int main(int argc, char **argv)you will get your command-line parameters in there.

如果您对标准进行编码,int main(int argc, char **argv)您将在那里获得命令行参数。

回答by vanza

main()allows you to call main with any number of parameters. main(void)forces you to call main with no parameters. So:

main()允许您使用任意数量的参数调用 main。main(void)强制您不带参数调用 main。所以:

main(foo, bar);

Is fine with main()but not with main(void)- the compiler generates an error.

可以,main()但不能main(void)- 编译器生成错误。

Now if you're specifically asking about the program's entry point, it doesn't really make a difference; in either case, you won't have the arguments to the program (argc, argv, envp) available.

现在,如果您专门询问程序的入口点,那实际上并没有什么区别;在任何一种情况下,您都不会获得程序的参数(argc、argv、envp)。

回答by Jerry Coffin

From a practical viewpoint, there's no real difference. With int main(void), you're explicitly stating that maintakes no parameters, so you can't invoke it with any. With int main(), you're leaving open the possibility of invoking mainwith some parameters.

从实际的角度来看,没有真正的区别。使用int main(void),您明确声明它不main带任何参数,因此您不能使用 any 来调用它。使用int main(),您将保留main使用某些参数进行调用的可能性。

However, except in strange situations like code golf or intentionally obfuscated code, you don't invoke mainanyway -- it's the entry point to the program, so it's automatically invoked by the startup code. The startup code will pass the command line arguments anyway, so your choices don't change how it's invoked, only whether you use or ignore the parameters that get passed.

但是,除了在诸如代码错误或故意混淆的代码等奇怪的情况下,您main无论如何都不会调用——它是程序的入口点,因此它会被启动代码自动调用。启动代码无论如何都会传递命令行参数,因此您的选择不会改变它的调用方式,只会改变您是否使用或忽略传递的参数。

The standard does specifically allow you to define mainwith or without parameters (§5.1.2.2.1/1):

该标准确实允许您定义main带或不带参数(第 5.1.2.2.1/1 节):

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

程序启动时调用的函数名为 main。实现声明没有此函数的原型。它应定义为返回类型为 int 且不带参数:

    int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

或带有两个参数(此处称为 argc 和 argv,尽管可以使用任何名称,因为它们对于声明它们的函数而言是本地的):

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

or equivalent;

或同等学历;

Though it's outside the tags specified, in C++ the situation is slightly different. In C, a function declaration like:

尽管它在指定的标签之外,但在 C++ 中情况略有不同。在 C 中,函数声明如下:

int f();

specifies that fis a function returning an int, but gives noinformation about the number or type of parameters fmight expect (this is included primarily for compatibility with old code -- at one time, this was the onlyway to declare a function in C). In C++, that same declaration explicitly declares fas a function that takes no parameters, so attempting to invoke fwith one or more parameters cannot invoke this function (it must either invoke another overload or produce an error if no suitable overload is found).

指定这f是一个返回 的函数int,但没有提供有关f可能期望的参数数量或类型的信息(包括这主要是为了与旧代码兼容——曾经,这是在 C 中声明函数的唯一方法)。在 C++ 中,同一声明显式声明f为不带参数的函数,因此尝试f使用一个或多个参数进行调用不能调用此函数(如果找不到合适的重载,它必须调用另一个重载或产生错误)。