11.6) An Example Of Arrays: Command Line Arguments
The main
function of C and C++ programs can be declared with two parameters:
int main(int argc, char* argv[]) { ... }
The parameter argc
is a count of how many items were specified on the command line that ran the program, including the name of the program itself. The parameter argv
is an array of C-style strings (pointers to char
) representing the items from the command line.
Here's an example of a command line that runs an executable called analyze:
./analyze experiment.data results.txt
For this command line, argc
will be 3, argv[0]
will be "./analyze", argv[1]
will be "experiment.data" and argv[2]
will be "results.txt"
See args.cpp for a demonstration