69
votes

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?

6
by reading your question even i am eager to know thisDevjosh
Downvoting (it does not make a difference, but just for you to note), for tagging the question with "C" when it is a pure C++ question (i.e. C does not support overloads, the title and first line make it explicit that it is C++). Avoid mixing languages that are not the same in questions around features that are clearly different.David Rodríguez - dribeas
@David, I purposedly put C tag because multiple version of main() are allowed in C also. Moreover, void main() is not supported in C++ but mostly it's valid in C. However, mentioning C++ in question, I have cleared by stand.iammilind
void main() is not valid C either if you talk about Standard C (C89, C90, C99).Nawaz
"if you pass no argument then 1st version should be called else the 2nd version". The one that is called is the one that the program contains. If the program contains the no-arg version, then the command line arguments are ignored. If the program contains the second version and there are no arguments, then argc is 1 and argv 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

6 Answers

65
votes

§3.6.1/2 (C++03) says

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

   int main() { /* ... */ }
   int main(int argc, char* argv[]) { /* ... */ }

You can use either of them. Both are standard compliant.

Also, since char *argv[] is equivalent to char **argv, replacing char *argv[] with char **argv doesn't make any difference.


But both the versions cannot co-exist at the same time ! (use case can be like: while running the binary from command prompt, if you pass no argument then 1st version should be called else the 2nd version).

No. Both versions cannot co-exist at the same time. One program can have exactly one main function. Which one, depends on your choice. If you want to process command-line argument, then you've to choose the second version, or else first version is enough. Also note that if you use second version, and don't pass any command line argument, then there is no harm in it. It will not cause any error. You just have to interpret argc and argv accordingly, and based on their value, you've to write the logic and the flow of your program.

20
votes

Windows and Unix have:

int main(int argc, char **argv, char **envp)

and Win32 apps have:

int WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);

and MacOS has:

int main(int argc, char **argv, char **envp, char **apple)

Don't forget that main is not usually the first thing the OS calls when executing a program. The main function is the function that is called by the run time environment. The address of the first instruction to execute is usually declared in some meta data, usually at the start if the executable file.

None of the above contradicts the C/C++ standard as far as I can tell, as long as there is only one, which makes sense since the OS wouldn't know which to call if there were more than one. Checking there is only one is not done in the compiler, it is done in the linker.

13
votes

Section 3.6.1.2 of both C++ Standard 1998 and 2003 editions states:

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.

Further,

The ISO C++ Standard (ISO/IEC 14882:1998) specifically requires main to return int. It has an explicit "shall" constraint upon well-formed programs.

Section § 3.6.1 ¶ 2:

It shall have a return type of int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* … */ }

and

int main(int argc, char* argv[]) { /* … */ }

So both versions of main are allowed by the standard and which one to use is left as an implementation preference of the programmer.

9
votes

Main was defined back in the C days. It's setup more along the rules of printf. Consider main as a varadic function:

int main(...)

The standard says that no arguments and two pointer arguments are okay. However, if the implementation wants to provide more arguments, the implementation is free to do so.

The reason you can't have two mains is the same reason you can't define a printf like function twice in a C program. Sure, printf supports differing arguments and changes it's behavior depending on which arguments are there, but it's not overloading in the C++ sense of the term.

7
votes

The standard says that main cannot be overloaded. It isn't mangled, and you cannot have two functions with the same unmangled name. This will cause a linking failure, I guess, but a compiler could want to add explicit checks in order to give clearer error messages.

int main(int argc, char **argv) and int main() should be the preferred signatures for it, but compilers are free to accept a main with different parameters.

0
votes

It is not possible to overload main() in C++ because. the compiler shown the following error:

error C2731: 'main' : function cannot be overloaded