Command Line Parameters

Most of the Unix commands are just executable images of programs. For example the command wc filename will print out the number of characters, words, and lines in the file filename. This command is just a compiled C program that takes "filename" as a command line parameter.

In general in C and C++ the main function receives two parameters:

  1. An integer, usually called argc, that indicates the total number of words on the command line (including the name of the program), and
  2. An array of C strings representing all the tokens separated by characters that appear on the command line. This parameter is usually called argv.

For example, if we write the command line

% foo a +765 -w3 roses then the main function of the image foo will receive 5 as value of argc and argv will be an array with the 5 C strings "foo", "a", "+765", "-w3", and "roses".

Here is the standard form of the main function of a C++ program (or of a C program):

int main( int argc, char * argv[] );

Here we see a simple program that prints out the command line parameters it receives.


ingargio@joda.cis.temple.edu