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

Command-line Arguments in C

Readings

Please read the following materials carefully:

Additional Notes

Acknowledgements

The following material on 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.

Introduction to argc and argv

As a systems-oriented programming language, C must provide a facility to pick up the command-line arguments that are specified when the program is executed. For example, when you type "cat file", the cat program has to be able to learn the filename string file.

C permits this to be done by providing an alternate definition of main(). The alternate declaration looks like :

        int main(int argc, char **argv)
or the equivalent:
        int main(int argc, char *argv[])
The name argc stands for "argument count", and this integer variable represents the number of separate tokens (i.e., "words") that were entered on the command line when the program was executed. It also gives the number of elements in argv.

The name argv stands for "argument vector". The argument vector may be considered to be an array of character strings. Each string in the array is one of the tokens entered on the command line when the program started to execute.

While you could theoretically give the parameters any name that you choose, the use of the names argc and argv is so standard that you should not use any other name.


This document is available on the World Wide Web as

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

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