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.
int main(int argc, char *argv[])
Where:
- argc - number of arguments to the program
- argv[0] - string which is the name of the executable
- argv[1] - string representing the next word typed on the command line
- argv[2] - string representing the next word typed on the command line
- etc
Program
/***********************************************
* File: args.c
* Author: R. Chang
* Date: ??
* Section: 0101
* Email: chang@cs.umbc.edu
*
* Program to demonstrate the syntax of command-line
* arguments
***********************************************/
#include
#include
int 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]);
}
return 0;
}
Output
linux1[94] % 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
linux1[95] %