Command Line Arguments in C

The purpose of this assignment is to get you familiar with the Unix environment -- emacs and gcc in particular -- and with command line arguments C.

  1. Log into your Unix account on tanner. Complete the first four sections (Topographical Conventions, Introduction, Tutorial One and Tutorial Two) of this Unix Tutorial for Beginners. Make sure you are comfortable navigating the Unix file system before moving on to the next step.

  2. In your home directory, create a directory called csc1600 (unless already there). In your csc1600 directory, create another directory called cmdargs.

  3. Change your current directory to csc1600/cmdargs. The work for the exercises below will be completed in this directory.

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 the emacs 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.

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 cmdargs
Pay careful attention to error and warning messages, and eliminate them all. To execute your program, use the command
    ./cmdargs 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 do so, 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).

Exercise 3

Write a program called sumargs.c that gets two numbers from the command line and prints out their sum. Compile your program to produce an executable called sumargs, then run your executable Save the program into a file called sumargs.c and run your executable using a command similar to the following:
    ./sumargs 7 11
The output in this case should be 1. 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 code, along with a sample output for Exercise 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 csc1600/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 ~/csc1600/cmdargs ~/csc1600/cmdargs-copy
A new directory called cmdargs-copy will be created in your csc1600 directory. You may now make any changes you want to the files in your cmdargs-copy directory, at any time.

Have fun!