tags:

views:

288

answers:

1

Hello

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

+20  A: 

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)
Brian R. Bondy
and what about auxv in linux?
osgx
@osgx : I'm not sure if others are possible but the 3 mentioned commonly above are supported by g++
Brian R. Bondy
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
@Dennis Zickefoose: Sounds like a great question for stackoverflow.com :)
Brian R. Bondy