In many C++ IDE's and compilers, when it generates the main function for you, it looks like this:
int main(int argc, char *argv[])
When I code C++ without an IDE, just with a command line compiler, I type:
int main()
without any parameters. What does this mean, and is it vital to my program?
This question is related to
c++
parameters
command-line-arguments
argv
argc
Suppose you run your program thus (using sh
syntax):
myprog arg1 arg2 'arg 3'
If you declared your main as int main(int argc, char *argv[])
, then (in most environments), your main()
will be called as if like:
p = { "myprog", "arg1", "arg2", "arg 3", NULL };
exit(main(4, p));
However, if you declared your main as int main()
, it will be called something like
exit(main());
and you don't get the arguments passed.
Two additional things to note:
main
. If a particular platform accepts extra arguments or a different return type, then that's an extension and should not be relied upon in a portable program.*argv[]
and **argv
are exactly equivalent, so you can write int main(int argc, char *argv[])
as int main(int argc, char **argv)
.argc
is the number of arguments being passed into your program from the command line and argv
is the array of arguments.
You can loop through the arguments knowing the number of them like:
for(int i = 0; i < argc; i++)
{
// argv[i] is the argument at index i
}
The parameters to main
represent the command line parameters provided to the program when it was started. The argc
parameter represents the number of command line arguments, and char *argv[]
is an array of strings (character pointers) representing the individual arguments provided on the command line.
int main();
This is a simple declaration. It cannot take any command line arguments.
int main(int argc, char* argv[]);
This declaration is used when your program must take command-line arguments. When run like such:
myprogram arg1 arg2 arg3
argc
, or Argument Count, will be set to 4 (four arguments), and argv
, or Argument Vectors, will be populated with string pointers to "myprogram", "arg1", "arg2", and "arg3". The program invocation (myprogram
) is included in the arguments!
Alternatively, you could use:
int main(int argc, char** argv);
This is also valid.
There is another parameter you can add:
int main (int argc, char *argv[], char *envp[])
The envp
parameter also contains environment variables. Each entry follows this format:
VARIABLENAME=VariableValue
like this:
SHELL=/bin/bash
The environment variables list is null-terminated.
IMPORTANT: DO NOT use any argv
or envp
values directly in calls to system()
! This is a huge security hole as malicious users could set environment variables to command-line commands and (potentially) cause massive damage. In general, just don't use system()
. There is almost always a better solution implemented through C libraries.
The main
function can have two parameters, argc
and argv
. argc
is an integer (int
) parameter, and it is the number of arguments passed to the program.
The program name is always the first argument, so there will be at least one argument to a program and the minimum value of argc
will be one. But if a program has itself two arguments the value of argc
will be three.
Parameter argv
points to a string array and is called the argument vector. It is a one dimensional string array of function arguments.
Both of
int main(int argc, char *argv[]);
int main();
are legal definitions of the entry point for a C or C++ program. Stroustrup: C++ Style and Technique FAQ details some of the variations that are possible or legal for your main function.
The first parameter is the number of arguments provided and the second parameter is a list of strings representing those arguments.
Source: Stackoverflow.com