9

What numbers of arguments are used for main? What variants of main definition is possible?

jschmier
  • 15,458
  • 6
  • 54
  • 72
osgx
  • 90,338
  • 53
  • 357
  • 513

1 Answers1

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
  • and what about auxv in linux? – osgx Mar 26 '10 at 17:13
  • 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