tags:

views:

242

answers:

4

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

+6  A: 

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.

bmargulies
+1 -- in C++ there is no difference, but there is a slight difference in C. It's minor though.
Billy ONeal
`"No difference"`, read Billy's post.
Prasoon Saurav
@Billy: This question has been tagged C not C++.
Prasoon Saurav
@Prasoon: Yes, I know. I still found that this answer is correct in C++ to be of merit. Also, just because there technically is a difference in C doesn't mean that it's a difference that is commonly used or matters. Therefore I think this is a good answer and deserves an upvote.
Billy ONeal
@Billy ONeal : This answer is not technically 100% correct(as you said) if you take C (tag) into consideration. But still I would not upvote this answer. I liked your answer BTW and have upvoted it.
Prasoon Saurav
@Prasoon: Well thank you :)
Billy ONeal
Well, OK. There is a difference if you actually write a call to main. One edit coming up.
bmargulies
+6  A: 

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

vanza
+1 -- note that `envp` is a nonstandard extension.
Billy ONeal
@Billy ONeal: But allowed by the standard in §5.1.2.2.1.1.
Matt Joiner
@Matt: I never said it wasn't allowed. I just said it was not in the standard.
Billy ONeal
+6  A: 

main() is a function named main which takes any number of arguments and returns an int. main(void) is a function named main taking no arguments and returning an int.

Strictly speaking, ANSI defines main to be int main(int argc, char **argv), but allows main() (in C89 only -- discouraged), int main(), or int main(void) for your program's entry point.

Special thanks to commenters @David and @James for checking standards.

Billy ONeal
In the C99 draft standard, 5.1.2.2.1 says that `int main(void)` and `int main(int argc, char *argv[])` (or equivalent) are both acceptable, along with any other implementation-defined forms.
David Thornley
@David: Edited that in.
Billy ONeal
`int main(void)` is acceptable in C89 as well (per 2.1.2.2).
James McNellis
@James: Thank you very much :)
Billy ONeal
A: 

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

Jerry Coffin
You seem to have a different copy of the standard than mine. In mine the paragraph ends with `or in some other implementation-defined manner`. And later it appears even that the return type of `int` is optional: `5.1.2.2.3 Program termination``If the return type of the main function is a type compatible with int, ...`
Jens Gustedt
@Jens: No, I simply didn't get into that, because it wasn't relevant to the question at hand. Yes, there might be other possible ways for main to be invoked, but at least those two are definitely allowed. Yes, it could conceivably return some other type, but the OP seems concerned only with parameters, not return type.
Jerry Coffin
@Jerry: Sorry if you felt offended. What follows from this phrase, though, that an implementation might even allow the version `int main()` with any number of parameters.
Jens Gustedt