prev

Command Line Arguments

next

When a program is run main can be passed the values that are typed in on the command line. These are passed in through int argc (the number of arguments) and char **argv (the array of arguments)

Source code
     /* Simple example of a program to print the command line arguments that are
      * passed to the program. 
      */
     #include <stdio.h>
     
     int main ( int argc, char** argv ) {
       int i;
     
       for (i = 0; i < argc; i++) {
         printf ("%s\n", argv[i]);
       }
     
       return 0;
     }

When this program is run with the following command line:

./cmdline foo blah
Then the following output is produced (notice that the argv[0] is the name of the program):
./cmdline
foo
blah