Command-Line Arguments

I l @ ve RuBoard

Command-Line Arguments

Before the modern graphical interface, there was the command-line interface. DOS and UNIX are examples. The command line is the line you type to run your program in a command-line environment. Suppose you have a program in a file named fuss . Then the command line to run it might look like this in UNIX:

 $ fuss 

Or it might look like this in DOS:

 C> fuss 

Command-line arguments are additional items on the same line.

 % fuss -r Ginger 

A C program can read those additional items for its own use (see Figure 11.6).

Figure 11.6. Command-line arguments.
graphics/11fig06.jpg

A C program reads these items by using arguments to main() . Listing 11.27 shows a typical example.

Listing 11.27 The echo.c program.
 /* echo.c -- main() with arguments */ #include <stdio.h> int main(int argc, char *argv[]) {    int count;    printf("The command line has %d arguments:\n", argc - 1);    for (count = 1; count < argc; count++)       printf("%d: %s\n", count, argv[count]);    printf("\n");    return 0; } 

Compile this program into an executable file called echo ; this is what happens when you run it:

 C>  echo Resistance is futile  The command line has 3 arguments: 1: Resistance 2: is 3: futile 

You can see why it is called echo, but you might wonder how it works. We'll explain now.

C compilers allow main() to have no arguments or else to have two arguments. The first argument is the number of strings in the command line. By tradition (but not by necessity), this int argument is called argc for arg ument count. The system uses spaces to tell when one string ends and the next begins. Therefore, the echo example has four strings, including the command name , and the fuss example has three. The second argument is an array of pointers to strings. Each string on the command line is stored in memory and has a pointer assigned to point to it. By convention, this array of pointers is called argv, for argument values. When possible (some operating systems don't allow this), argv[0] is assigned the name of the program itself. Then argv[1] is assigned the first following string, and so on. For our example, we have the following relationships:

argv[0] points to echo (for most systems)
argv[1] points to Resistance
argv[2] points to is
argv[3] points to futile

The program in Listing 11.27 uses a for loop to print each string in turn . Recall that the printf() %s specifier expects the address of a string to be provided as an argument. Each element, argv[0] , argv[1] , and so on, is just such an address.

With pre-ANSI C, use this form to declare argc and argv :

 main(argc, argv) int argc; char *argv[]; 

The form is the same as for any other function having formal arguments. Many programmers use a different declaration for argv :

 int main(int argc, char **argv) 

This alternative declaration for argv really is equivalent to char *argv[] . It says that argv is a pointer to a pointer to char . The example comes down to the same thing. It had an array with seven elements. The name of the array is a pointer to the first element, so argv points to argv[0] , and argv[0] is a pointer to char . Hence, even with the original definition, argv is a pointer to a pointer to char . You can use either form, but we think that the first more clearly suggests that argv represents a set of strings.

Incidentally, many environments, including UNIX and DOS, allow the use of quotation marks to lump several words into a single argument. For example, the command

 echo "I am hungry" now 

would assign the string "I am hungry" to argv[1] and the string "now" to argv[2] .

Command-Line Arguments in Integrated Environments

Integrated Windows environments like Metrowerks CodeWarrior, Microsoft Visual C++, and Borland C/C++ don't use command lines to run programs. However, some have menu selections that enable you to specify a command-line argument. In other cases, you may be able to compile the program in the IDE, and then open an MS-DOS window to run the program in command-line mode.

Command-Line Arguments with the Macintosh

The Macintosh operating system doesn't use command lines, but Metrowerks CodeWarrior and Symantec C++ enable you to simulate a command-line environment with the ccommand() function. Use the console.h header file and start your programs like this:

 #include <stdio.h> #include <console.h> int main(int argc, char *argv[]) {    ...      /* variable declarations */    argc = ccommand(&argv);    ... } 

When the program reaches the ccommand() function call, it puts a dialog box onscreen and provides a box in which you can type a command line. The command then places the command-line words in the argv strings and returns the number of words. The current project name will appear as the first word in the command-line box, so you should type the command-line arguments after that name. The ccommand() function also enables you to simulate redirection.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net