Programming in C

(Barry) #1

380 Chapter 17 Miscellaneous and Advanced Features


Command-Line Arguments


Many times, a program is developed that requires the user to enter a small amount of
information at the terminal.This information might consist of a number indicating the
triangular number that you want to have calculated or a word that you want to have
looked up in a dictionary.
Rather than having the program request this type of information from the user, you
can supply the information to the program at the time the program is executed.This
capability is provided by what is known as command-line arguments.
As pointed out previously, the only distinguishing quality of the function mainis that
its name is special; it specifies where program execution is to begin. In fact, the function
mainis actually calledupon at the start of program execution by the C system (known
more formally as the runtimesystem), just as you call a function from within your own C
program.When maincompletes execution, control is returned to the runtime system,
which then knows that your program has completed execution.
When mainis called by the runtime system, two arguments are actually passed to the
function.The first argument, which is called argcby convention (for argument count), is
an integer value that specifies the number of arguments typed on the command line.The
second argument to mainis an array of character pointers, which is called argvby con-
vention (for argument vector).There are argc+ 1 character pointers contained in this
array, where argcalways has a minimum value of 0 .The first entry in this array is a
pointer to the name of the program that is executing or is a pointer to a null string if
the program name is not available on your system. Subsequent entries in the array point
to the values that were specified in the same line as the command that initiated execu-
tion of the program.The last pointer in the argvarray,argv[argc], is defined to be
null.
To access the command-line arguments, the mainfunction must be appropriately
declared as taking two arguments.The conventional declaration that is used appears as
follows:
int main (int argc, char *argv[])
{
...
}
Remember, the declaration of argvdefines an array that contains elements of type
“pointer to char.” As a practical use of command-line arguments, recall Program 10.10,
which looked up a word inside a dictionary and printed its meaning.You can make use
of command-line arguments so that the word whose meaning you want to find can be
specified at the same time that the program is executed, as in the following command:
lookup aerie
This eliminates the need for the program to prompt the user to enter a word because it
is entered on the command line.
Free download pdf