CSC 161 Grinnell College Spring, 2010
 
Imperative Problem Solving and Data Structures
 

Command-line Arguments in C

Goals

This laboratory exercise provides practice with arrays of string literals in C. In particular, you will gain experience processing command-line arguments received by C programs.

Prerequisites:

Arrays in C, Strings in C, Pointers in C.

Index

Acknowledgements

Some of this material (in particular, Part B: Introduction to argc and argv) was used in CSC180 at the University of Toronto. Alan Rosenthal created the original laboratories (Fall 2001, Fall 2002). They were later revised by Tom Fairgrieve (Fall 2003, Fall 2004) and Marge Coahran (Fall 2004, Fall 2005). In January 2007, Ms. Coahran brought them to Grinnell College with permission from both Alan and Tom.

Most other parts of this lab originally were written and edited by Marge Coahran in March 2007 and revised through March 2008.

Part A: Arrays of String Literals

  1. Write a program planet.c that allows the user to enter the name of a planet, and then prints a message stating that planet's number when counted outward from the sun. If the user enters a datum that is not a planet, your program should print a message to that effect.

    You will probably want to begin your program by declaring an array of string literals, as shown below. You can then refer to the name of planet i with planets[i].

       char* planets[8] = {"Mercury", "Venus", "Earth", "Mars",
                          "Jupiter", "Saturn", "Uranus", "Neptune"};
    

    An example run might look like the following:

    Enter a planet name: Earth
    Earth is planet 3, counting outward from the Sun.
    

Part B: Introduction to argc and argv

As discussed in the reading for this lab, the main function in C has the form:

        int main(int argc, char **argv)
or the equivalent:
        int main(int argc, char *argv[])

In the following exercises, you are to use these string parameters to retrieve and process information entered by the user on the command line.

  1. Write a small program command-example.c that uses the alternate main() declaration syntax given above. In the body of your main function, just print the value of argc. Compile your program and run it with various numbers of arguments. For example, execute the commands:

       ./command-example x y z a b c
       ./command-example
       ./command-example x a
    

    Is the output value what you expected?

  2. Modify your program command-example.c so that it also prints each of the strings stored in the array referred to by argv. Recall that argv[i] can be used as the name of the string containing the ith argument.

  3. Write a program that expects two command-line arguments, besides the program name. The first thing such a program should do is check whether the correct number of arguments were received. If not, the program should print an error message, such as "Usage: progname arg1 arg2" and then terminate. You can terminate your program with either "return 1" or "exit(1)". Both of these options will cause your program to return the value 1, rather than the value 0. This is the conventional way to indicate a program failure of some sort. To use the c library function exit(), you will also need to add "#include <stdlib.h>" at the top of your program.

    Ordinarily, if a program receives the correct number of arguments, it should simply continue. However, for this exercise, your program should print another message congratulating the user on his or her correct usage.

    Be sure to test your program, giving it various numbers of arguments. In particular, make sure that it gives the correct message when you enter one, two, or three arguments in addition to the program name.

Part C: Processing Command-Line Arguments

  1. Modify your program planet.c from Step 1 such that it gets its user input as a command-line argument, rather than prompting the user to input a value.

    An example run might look like the following, where quine$ is the shell prompt.

       ./planet Earth
       Earth is planet 3, counting outward from the Sun.
    
  2. Write a program chgcase that accepts two command-line arguments. The first argument should be an option string: the valid strings are "-u", "-l", and "-p". The second argument can be any word.

    Your program should first check whether the appropriate number of arguments have been supplied. If not, your program should display a usage message and terminate. Next, your program should check whether argument 1 is a valid option string, as defined above. If not, again print an informative error message. Finally, if all is well so far, your program should output argument 2 after transforming it as follows:

    For example, your program should output

    Hint: The C library ctype.h has several functions for processing characters. See, for example, Eric Huss, The C Library Reference Guide, University of Illinois Student Chapter, 1997.

  3. Modify your program chgcase.c such that it may be invoked with its two command-line arguments in either order. In other words, you should determine which argument is the option string based on the fact that the option string begins with a distinguished character (the hyphen). If both of the supplied arguments begin with a hyphen, your program should print an informative error message.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/161.sp10/labs/lab-command-line.shtml

Part B: created Fall 2001 by Alan Rosenthal
Part B: revised Fall 2002 by Alan Rosenthal
Part B: revised Fall 2003 and Fall 2004 by Tom Fairgrieve
Part B: revised Fall 2004 and Fall 2005 by Marge Coahran
Part B: brought to Grinnell College January 2007 by Marge Coahran with permission from Alan and Tom
Part B: revised 2007, Spring 2008 by Marge Coahran
Other Parts: created 2007 by Marge Coahran
Other Parts: Spring 2008 by Marge Coahran
Full Lab: reformatted 11 January 2000 by Henry M. Walker
revised 3 April 2009 by Henry M. Walker
last revised 2 April 2010 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.