Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

main

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

like image 851
osgx Avatar asked Mar 26 '10 17:03

osgx


People also ask

Can Main have arguments in C?

Yes, we can give arguments in the main() function. Command line arguments in C are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution. The argc and argv are the two arguments that can pass to main function.

What are arguments to main?

There are at least two arguments to main : argc and argv . The first of these is a count of the arguments supplied to the program and the second is an array of pointers to the strings which are those arguments—its type is (almost) 'array of pointer to char '.

How many arguments can be passed to main from the command line in C?

There are two parameters passed to main(). The first parameter is the number of items on the command line (int argc). Each argument on the command line is separated by one or more spaces, and the operating system places each argument directly into its own null-terminated string.

What is the main () function in C?

Every C program has a primary (main) function that must be named main. If your code adheres to the Unicode programming model, you can use the wide-character version of main, wmain. The main function serves as the starting point for program execution.


1 Answers

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)
like image 115
Brian R. Bondy Avatar answered Oct 11 '22 09:10

Brian R. Bondy