2 valid versions of main()
exist in C++
:
int main() // version 1
int main(int argc, char **argv) // version 2
But both overloads cannot coexist at the same time. Why not? (Potential use case: while running the program from the terminal, if no arguments are passed the first version is called, otherwise the second version is.)
Does the compiler perform a special check to allow just one version per binary?
C
tag because multiple version ofmain()
are allowed inC
also. Moreover,void main()
is not supported in C++ but mostly it's valid inC
. However, mentioningC++
in question, I have cleared by stand. – iammilindvoid main()
is not valid C either if you talk about Standard C (C89, C90, C99). – Nawazargc
is 1 andargv
contains a pointer to the program name followed by a null pointer. So even if you could overload it, you wouldn't want that behavior, since even with no arguments there's still an argc/argv pair. – Steve Jessop