Question

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

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top