What numbers of arguments are used for main
? What variants of main
definition is possible?
Asked
Active
Viewed 3,103 times
9
-
Right there in the "Related" links: http://stackoverflow.com/questions/1621574/mains-signature-in-c Also, you tagging is inconsistent with your title. – dmckee --- ex-moderator kitten Mar 26 '10 at 17:49
-
added `c++` tag for consistency with title – jschmier Mar 26 '10 at 18:08
1 Answers
25
C++ Standard: (Source)
The C++98 standard says in section 3.6.1.2
It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both the following definitions of main: int main() and int main(int argc, char* argv[])
Commonly there are 3 sets of parameters:
- no parameters /
void
int argc, char ** argv
int argc, char ** argv, char ** env
Where argc
is the number of command lines, argv
are the actual command lines, and env
are the environment variables.
Windows:
For a windows application you have an entry point of WinMain with a different signature instead of main.
int WINAPI WinMain(
__in HINSTANCE hInstance,
__in HINSTANCE hPrevInstance,
__in LPSTR lpCmdLine,
__in int nCmdShow
);
OS X: (Source)
Mac OS X and Darwin have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary:
int main(int argc, char **argv, char **envp, char **apple)

Community
- 1
- 1

Brian R. Bondy
- 339,232
- 124
- 596
- 636
-
-
1@osgx : I'm not sure if others are possible but the 3 mentioned commonly above are supported by g++ – Brian R. Bondy Mar 26 '10 at 17:16
-
I have never understood the requirement that `main` return `int`. `main` is the only function explicitly allowed to have an implicit return value. Why go out of your way to allow `main` to pretend to be `void`, rather than simply allowing it to be `void` to begin with? – Dennis Zickefoose Mar 26 '10 at 19:19
-
@Dennis Zickefoose: Sounds like a great question for stackoverflow.com :) – Brian R. Bondy Mar 26 '10 at 20:23