Command-Line Arguments

Introduction

Background

Command-line arguments are passed to the main function of a program as an array of strings. For example, if you write a program
    int main(int argc, char * argv[])
    { ...}
and invoke the executable using the command line
    ./a.out one two three
then the value 4 will be passed to argc in the main function, and the string array argv will be allocate memory space as in the diagram below.

       

To Do

Log into your Unix account on tanner. In your home directory, create another directory called systems. This is the work directory for this class. In your systems directory, create another directory called cmdargs. Change your current directory to systems/cmdargs, then complete exercises 1 through 3 below.

Exercise 1.

C, C++ and Java allow you to input values into your program through the command line. You will only need to declare the main function in a slightly different form. In C (and C++), this is

      int main( int argc, char * argv[] )
Here argc is the number of command line arguments (including the name of the executable), and argv is an array of the arguments.

Invoke emacs (or your favorite Unix editor) to write a program called cmdargs.c as follows:

     emacs cmdargs.c
Remember to save your code frequently with CTRL-X CTRL-S, use CTRL-G when stuck in the editor, and CTRL-X CTRL-C to exit the editor. If you wish to undo a change triggered by a command, use CTRL-X U.

Type the following program in your file cmdargs.c:

    int main( int argc, char * argv[] )
    {
        int i;
        printf("\nYour program name is %s\n", argv[0]);
        printf("Your arguments are: \n");
        for(i = 1; i < argc; i++)
            printf("\t %s starts with %c\n", argv[i], argv[i][0]);
        return 0;
    }
Save your program (CTRL-X CTRL-S) and then exit (CTRL-X CTRL-C). Compile your code using the command
     gcc cmdargs.c -o xcmdargs
Pay careful attention to error and warning messages, and eliminate them all. To execute your program, use the command
    ./xcmdargs let us see what this does
Inspect the output of your code and analyze the code to answer the following questions
  1. What data type is argc?
  2. What value does argc take when the program gets invoked? How is it related to the command line you used to invoke the program?
  3. What data type is argv?
  4. What data type is argv[0]? What about argv[1]?
  5. What data type is argv[0][0]? What about argv[0][1]?
  6. What gets stored in argv[0]? What gets stored in argv[1]?
  7. What values are stored in argv when the program gets invoked? Draw a diagram (memory boxes and arrows for pointers) illustrating the memory space occupied by argv.

Exercise 2

Use the manual pages on the Sun system or follow this link to learn about the C function atoi. To use the manual pages, type in the command
    man atoi
You will find out that the atoi function takes a string (char * in C) as an argument, and converts it to an integer. For instance, in the C statement
    num = atoi("123")
the atoi function takes as argument the string "123" and returns the integer value 123 (one hundred and twenty three) in the integer variable num (which must be defined earlier in your code).

Write a small program (say, atoi.c) to test the functionality of the atoi function and make sure that it works as expected. Keep it simple. For example, invoke the function once as shown above, then print out the return value.

Note: Do not underestimate the importance of learning to write simple test code. This is the fastest way to learn how a new function works, and it will save you a lot of debugging time in the long run.


Exercise 3

Write a program called sumargs.c that receives two integers from the command line and prints out their sum. Compile your program to produce an executable called xsumargs, then run it using a command similar to the following:
    ./xsumargs 7 11
The output in this case should be 18. Have your program print an error message in case the command line is invalid:
    if(argc < 3)
    {
        printf("Invalid command line: supply two integers \n");
        exit(1);
    }
Note that you'll need to use the atoi function to convert the arguments (which are strings) to integer values.

Submission Instructions

Turn in a printout copy of your answers to Exercise 1, and printouts of your code and a sample output for each of Exercises 2 and 3. (You will need to copy and paste your code into a text or a Word document in Windows, since you cannot print it directly from Unix.)

Leave the source code for all exercises in your directory systems/cmdargs, and do not make any changes to these files after the due date for this assignment. If you wish to continue working on these exercises after the due date, make a copy of your directory cmdargs using the following Unix commands:

    cp -r ~/systems/cmdargs ~/systems/cmdargs-copy
A new directory called cmdargs-copy will be created in your systems directory. You may now make any changes you want to the files in your cmdargs-copy directory, at any time.

Have fun!