Tutorial
C++
Command Line Arguments


Invoking a Program with Command-Line Arguments

For those of you who have worked in Unix or a Coomand Prompt window under Windows, you have probably entered a command such as:
cd assignment1
		
i.e., a command to change the current working directory to assignment1. The above command actually consists of two components: Note running the command is analogous to calling a function-- in both cases This is not a coincidence-- in essence your program is viewed by the operating system as just another function to be called. There obviously is more going on then a simple function call within a program but eventually the OS does indeed make a function call to the program's entry point (which for our purposes is the main function).

(As an aside, similarly, the program returns to the OS in much the same way, as can be seen from the return statement in main. The return value is information passed back by the program to the OS about the success/failure of the program, and is called the program exit status.)

Thus, we see that a program can be invoked with arguments that provide information to the program from the user. We call these command line arguments.

In order for this information to be of any value to the program, we must be able to access the command line arguments from within the program.

Accessing the Command Line Arguments From the Program

As we saw above, the command line consists of The user types the program name and arguments on the command line, and the operating system then parses the command line, breaking it up to it individual parts-- the program name and each argument (where again, for our purposes, the arguments are separated by whitespace). Each of these components-- the program name and the individual arguments-- are treated as a C-like string (char *) by the OS, placed into an array of such strings (i.e., char *[] or, equivalently, char **) and passed together with the number of elements in the array as arguments to main, just as arguments would be passed to any other function.

Thus, the proper signature to main is

int main(int argc, char **argv)
		
or, equivalently,
int main(int argc, char *argv[])
		
This is similar to ANY function that accepts an array (and its corresponding size) as a parameter, except for the fact that whereas we usually place the array first and the number of elements second, it is reversed here (which if you think about it may be the sensible way-- after all, first you have to know the number of elements, and then you can process the elements).

Note: By convention, the arguments are named argc (for argument count) and argv (for argument vector -- or array), though of course you could name them anything you want (as long as you maintain the order of count first, then array).

The argv Array

Vector is nothing more than a fancy name for a 1-dimensional array, and that is exactly what argv is: a 1-dimensional array of C-style character strings. The 0'th element-- which is ALWAYS present-- is the name of the program as invoked on the command line, arguments 1 ... argc-1 are the individual command-line arguments.

An Example-- finder

finder is a program that accepts a string (the program calls it the pattern and an arbitrary number of file names and prints out the names of all those files that contain the string.

The program accepts an arbitrary number of arguments -- the first (argv[1] don't forget, the zeroth is the name of the program) is the pattern, and the rest are the names of the files to be searched.

The program also contains the standard logic for detecting and notifying the user if there are an invalid number of arguments, as well as logic for returning an appropriate exit status: 0 if the pattern was found, 1 if it wasn't found in any of the files, and 2 if there were an invalid # of arguments.

Some Final Notes

Code for this Tutorial