C Language / Lesson 9 Back to Index or to Previous Page or to Next Page

 

Passing parameters at the Command Line

A command line is a text line submitted to the operating system (by the user or by another program) in order to start the execution of a command (usaly a program).
A command line is not a program line
The command line consists of arguments which –according to UNIX and C specifications- are separated with spaces. The first argument is the command name (program name) and the rest are the so called passing parameters. When a program is executed, the command line which launched the execution is passed by the operating system to it. The C language start up code -which is invisible to you but always executed before the main( ) function starts - sees that the information from the command line is passed to main( ). This is done with the use of two arguments passed to main ( ) as shown below.

An example with 3 passing parameters i.e. 4 arguments at the command line.


main(int argc , char **argv)
{ . . .


///// Display all the arguments (incl. the program name) ///
void main(int argc, char **argv)
{ int i;

for (i=0 ; i<argc ; ++i )
   printf("%s\n" , argv[i] ) ;

} ;

 

Previous page or Next page or Back to Index