UMBC CS 201, Fall 98
Command Line Arguments
When a C program begins execution the shell passes parameters to the
main program function that reflect the command line that began the
program.
void main(int argc, char *argv[])
Where:
- argc - number of arguments to the program
- argv[0] - name of the program
- argv[1] - string representing the 1st argument to the command
- argv[2] - string representing the 2nd argument to the
command
- etc
Program
#include
#include
main(int argc, char *argv[])
{
int i;
printf("%s called with %d arguments:\n",argv[0],
argc);
for(i = 0; i < argc; i++)
{
printf(" argv[%d] = %s\n", i, argv[i]);
}
}
Output
% cc args.c
% a.out foo bar 13.3 mumble 2
a.out called with 6 arguments:
argv[0] = a.out
argv[1] = foo
argv[2] = bar
argv[3] = 13.3
argv[4] = mumble
argv[5] = 2
CSEE
|
201
|
201 F'98
|
lectures
|
news
|
help
Sunday, 08-Nov-1998 15:57:31 EST