CS16, Spring 2010

lab04—notes on command line args


 

Here's a review of command line arguments from lab03 and various lecture notes

Purpose of command line arguments

 

How to use command line arguments

To use command line arguments, we use a different first line of the main program:
Instead of:
 int main()
we use:
  int main(int argc, char *argv[])

Examples:

command line argc argv[0] argv[1] argv[2] argv[3] argv[4] argv[5] argv[6]
./areaOfSquare
1 "./areaOfSquare" undefined undefined undefined undefined undefined undefined
./areaOfSquare 9
2 "./areaOfSquare" "9" undefined undefined undefined undefined undefined
./boxOfStars 5 4
3 "./boxOfStars" "5" "4" undefined undefined undefined undefined
./p1 foo bar fum 23 4.5
6 "./p1" "foo" "bar" "fum" "23" "4.5" undefined

 

Checking argc before accessing argv

If we have defined argc and argv by putting  int main(int argc, char *argv[]) as the first line of our main, then argc is always at least 1, and argv[0] always exists and is the name of the program (as typed on the Unix command line.)

However, argv[1], argv[2], don't necessarily exist, unless there are sufficient items on the command line.

So, before accessing the value of argv[1], argv[2], etc. it is necessary to always first check the value of argc.

We typically do that with code like the following

if (argc != 3)
{
printf("Usage: ./areaOfRectangle width height\n");
return 1;
}

This code prints a Usage message, which is a special kind of error message telling the user that he/she didn't use the program correctly—that is, the program was expecting three things on the command line:

  1. the program name (in this case, ./areaOfRectangle)
  2. a value for width
  3. a value for height

The program returns the value 1, because that signals an error condition.

A side note:

Sometimes, to make things a little easier to maintain as we share code among different programs, we can write it this way:

if (argc != 3)
{
printf("Usage: %s width height\n", argv[0]);
return 1; }

In this case, the %s indicates that we are printing a string ( a char *) value, and the argv[0] indicates that we want to print the name of the program.

Converting command line arguments to int or double values

When accessing command line arguments that are integers or floating-point numbers, we typically need to use atoi() or atof().

For example consider the code listed below, especially the part in bold
(the full program is available at raiseToPower.c):

// raiseToPower.c
// A program that illustrates command line args with both int and double
// P. Conrad for CS16, Winter 2010

#include <stdio.h>
#include <stdlib.h> // for atoi, atof

int main(int argc, char *argv[]) // for command line args
{
  
  double base;
  int exponent;
  ...

  
  // check command line args

  if (argc != 3)
    {
      printf("Usage: raiseToPower base exponent\n");
      return 1;
    }
  
  // convert command line args

  base = atof(argv[1]); // use atof with double
  exponent = atoi(argv[2]); // use atoi with integer

  ...
   
   

As we can see, if we are converting a command line argument to use it as an integer, we use atoi, and if we use it as a double, we use atof. In both cases, we need to check argc first, and in both cases, we need to #include <stdlib.h>

A final note:

For more information: