There are several forms of the main() function. This one is defined in such a way as to accommodate capturing and using command line arguments within the main function block.
That is, argc
will contain an integer value indicating how many command line arguments were used to invoke the executable. argv[]
will contain string representations of the arguments, with the name of the executable itself being in position 0 of the array, if there are arguments (or switches) following the executable name, they will be contained in array positions matching the order in which they appear on the command line.
For example: for an executable named GetName.exe, and invoked with these arguments:
GetName.exe -t -s
argc == 3
argv[0] == "GetName.exe"
argv[1] == "-t"
argv[2] == "-s"
Other than that, Its by definition. Why is water wet, and comprised of H2O?
EDIT to answer question in comments:
argc. argv as described provide a way for the executable to aquire, and use command line arguments. Here is a quick example of how that works:
Assuming the name of the executable GetName.exe, and again, called like:
GetName.exe -t -s
And given the following code:
#include <stdio.h>
int main (int argc, char *argv[])
{
int i=0;
printf("\ncmdline args count=%s", argc);
/* First argument is executable name only */
printf("\nexe name=%s", argv[0]);
for (i=1; i< argc; i++)
{
printf("\narg%d=%s", i, argv[i]);
}
printf("\n");
return 0;
}
The output would be:
cmdline args count=3
exe name=./GetName.exe
arg1=-t
arg2=-s