Wednesday, September 29, 2010

argc and argv—Arguments to main( )

Sometimes it is useful to pass information into a program when you run it. Generally,
you pass information into the main() function via command line arguments. A
command line argument is the information that follows the program's name on the
command line of the operating system. For example, when you compile a program,
you might type something like the following after the command prompt:
cc program_name
where program_name is a command line argument that specifies the name of the
program you wish to compile.
There are two special built-in arguments, argv and argc, that are used to receive
command line arguments. The argc parameter holds the number of arguments on

the command line and is an integer. It is always at least 1 because the name of the
program qualifies as the first argument. The argv parameter is a pointer to an array of
character pointers. Each element in this array points to a command line argument. All
command line arguments are strings—any numbers will have to be converted by the
program into the proper internal format. For example, this simple program prints
Hello and your name on the screen if you type it directly after the program name.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if(argc!=2) {
printf("You forgot to type your name.\n");
exit(1);
}
printf("Hello %s", argv[1]);
return 0;
}
If you called this program name and your name were Tom, you would type name Tom
to run the program. The output from the program would be Hello Tom.
In many environments, each command line argument must be separated by a space
or a tab. Commas, semicolons, and the like are not considered separators. For example,
run Spot, run
is made up of three strings, while
Herb,Rick,Fred
is a single string since commas are not generally legal separators.
Some environments allow you to enclose within double quotes a string containing
spaces. This causes the entire string to be treated as a single argument. Check your
operating system documentation for details on the definition of command line
parameters for your system.
You must declare argv properly. The most common method is
char *argv[];

The empty brackets indicate that the array is of undetermined length. You can now
access the individual arguments by indexing argv. For example, argv[0] points to the
first string, which is always the program's name; argv[1] points to the first argument,
and so on.
Another short example using command line arguments is the program called
countdown, shown here. It counts down from a starting value (which is specified on
the command line) and beeps when it reaches 0. Notice that the first argument
containing the number is converted into an integer by the standard function atoi() .If
the string "display" is the second command line argument, the countdown will also be
displayed on the screen.
/* Countdown program. */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char *argv[])
{
int disp, count;
if(argc<2) {
printf("You must enter the length of the count\n");
printf("on the command line. Try again.\n");
exit(1);
}
if(argc==3 && !strcmp(argv[2], "display")) disp = 1;
else disp = 0;
for(count=atoi(argv[1]); count; --count)
if(disp) printf("%d\n", count);
putchar('\a'); /* this will ring the bell */
printf("Done");
return 0;
}
Notice that if no command line arguments have been specified, an error message is
printed. A program with command line arguments often issues instructions if the
user attempts to run the program without entering the proper information.
To access an individual character in one of the command line arguments, add a
second index to argv. For example, the next program displays all of the arguments
with which it was called, one character at a time:

#include <stdio.h>
int main(int argc, char *argv[])
{
int t, i;
for(t=0; t<argc; ++t) {
i = 0;
while(argv[t][i]) {
putchar(argv[t][i]);
++i;
}
printf("\n");
}
return 0;
}
Remember, the first index accesses the string, and the second index accesses the
individual characters of the string.
Normally, you use argc and argv to get initial commands into your program. In
theory, you can have up to 32,767 arguments, but most operating systems do not allow
more than a few. You typically use these arguments to indicate a filename or an option.
Using command line arguments gives your program a professional appearance and
facilitates its use in batch files.
When a program does not require command line parameters, it is common
practice to explicitly declare main() as having no parameters. For C programs this is
accomplished by using the void keyword in its parameter list. (This is the approach
used by the programs in Part One of this book.) However, for C++ programs you
may simply specify an empty parameter list. In C++, the use of void to indicate an
empty parameter list is allowed, but redundant.
The names argc and argv are traditional but arbitrary. You may name these two
parameters to main() anything you like. Also, some compilers may support additional
arguments to main() , so be sure to check your user's manual.

No comments:

Post a Comment