Command line arguments

When a user runs a program by typing in a command (e.g. ./labex1 ) they have the option of passing one or more arguments to the program at the same time.

For example, if I wrote a program called copyfile, I might want to be able to specify the name of the file to be copied and the name of the new (duplicate) file, e.g.
./copyfile myOriginalFile myNewCopy

These extra arguments are simply passed as parameters to the main routine in your program. (In fact, this is exactly what linux programs like ls, cp, rm, etc. use.)

The main routine actually receives two parameters:

The precise syntax is
int main(int argc, char *argv[])

So, if my program executable was named expt, and I typed the command
./expt hi there 161

The sample program below accepts any number of command line arguments and simply prints them all out
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
   cout << "There are " << argc << " command line arguments:" << endl;
   for (int i = 0; i < argc; i++) {
       cout << "(" << i << "):" << argv[i] << endl;
   }
}