开发者

Difference between main(void) and main() in C [duplicate]

This question already has answers here: 开发者_Go百科 What should main() return in C and C++? (19 answers) Closed 7 years ago.

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()?


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

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

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


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

main(foo, bar);

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

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.


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

However, except in strange situations like code golf or intentionally obfuscated code, you don't invoke main anyway -- 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.

The standard does specifically allow you to define main with or without parameters (§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:

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

    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:

int f();

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜