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

Archit picture Archit · Sep 14, 2010 · Viewed 31.3k times · Source

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

Answer

bmargulies picture bmargulies · Sep 14, 2010

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.