27

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

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Archit
  • 309
  • 1
  • 3
  • 8

3 Answers3

13

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
  • 91,317
  • 38
  • 166
  • 290
  • +1 -- in C++ there is no difference, but there is a slight difference in C. It's minor though. – Billy ONeal Sep 14 '10 at 16:55
  • 1
    `"No difference"`, read Billy's post. – Prasoon Saurav Sep 14 '10 at 16:55
  • @Billy: This question has been tagged C not C++. – Prasoon Saurav Sep 14 '10 at 16:56
  • 2
    @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 Sep 14 '10 at 16:58
  • @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 Sep 14 '10 at 17:02
  • Well, OK. There is a difference if you actually write a call to main. One edit coming up. – bmargulies Sep 14 '10 at 19:11
8

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
  • 8,729
  • 1
  • 28
  • 33
3

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
  • 437,173
  • 71
  • 570
  • 1,035
  • @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 Sep 14 '10 at 18:37
  • 1
    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 Sep 14 '10 at 19:52
  • @JerryCoffin , quick question. How would you invoke an Overload if the function is f( ) ? I was looking at this locklessinc(dot)com/articles/overloading/ example but it seems like you need a parameter for that. Thanks – BluePython Sep 13 '15 at 16:42
  • @BluePython: I'm not sure I follow what you're asking. – Jerry Coffin Sep 13 '15 at 20:54