How many arguments does main() have in C/C++

心不动则不痛 提交于 2020-01-01 09:00:02

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/2525183/how-many-arguments-does-main-have-in-c-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!