UMBC CS 201, Fall 06
UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

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:

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 <stdlib.h> #include <stdio.h> 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] % [an error occurred while processing this directive]
Tuesday, 22-Aug-2006 07:14:11 EDT