Hardware Reference
In-Depth Information
Figure 7.9 Output produced by the example menu program
and argv . When main is called, argc is the number of elements in argv , and
argv is an array of pointers to the strings which appear in the command line.
The following demonstration illustrates the method of passing parameters:
/* argdemo.c */
#include<stdio.h>
main(argc, argv)
char *argv[];
int argc;
{
int i
printf("argc: %d\n", argc);
for(i = 0;i < argc; i++)
printf("argv[%d]: %s\n", i ,arqv[i]);
}
After compilation into an executable program the routine is invoked from the
MS-DOS in the following manner:
ARGDEMO ONE TWO THREE
The parameters to be passed are, in this case, the strings: one, two, and three.
The program generates the following output:
argc: 4
argv[0]: C\MC\BIN\ARGDEMO.EXE
argv[l]: one
argv[2]: two
argv[3]: three
The total number of parameters passed is given in argc . In this case, four
parameters have been passed (including the current directory and program name
which appears as argv[0] ). The three strings (one, two, and three) appear as
argv[1] , argv[2] , and argv[3] .
The following code fragment shows how the technique of parameter passing
can be used to display the contents of a named file.
Search WWH ::




Custom Search