argc and argv—Arguments to main( ). McGraw-Hill The Complete Reference Fourth Edition
广告
广告
144
C + + : T h e C o m p l e t e R e f e r e n c e
} case '\n': s[t] = '\0'; /* terminate the string */ return p; case '\b': if(t>0) t--; break; default: s[t] = ch;
}
} s[79] = '\0'; return p;
The xgets( ) function must be called with a character pointer. This, of course, can be the name of a character array, which by definition is a character pointer. Upon entry,
xgets( )
establishes a for loop from 0 to 79. This prevents larger strings from being entered at the keyboard. If more than 80 characters are entered, the function returns.
(The real gets( ) function does not have this restriction.) Because C/C++ has no built-in bounds checking, you should make sure that any array used to call xgets( ) can accept at least 80 characters. As you type characters on the keyboard, they are placed in the string.
If you type a backspace, the counter t is reduced by 1, effectively removing the previous character from the array. When you press
ENTER
, a null is placed at the end of the string, signaling its termination. Because the actual array used to call xgets( ) is modified, upon return it contains the characters that you type.
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
C h a p t e r 6 : F u n c t i o n s
145 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
146
C + + : T h e C o m p l e t e R e f e r e n c e 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:
广告