Tuesday, March 10, 2009

C Program with Arguments

There are number of executable programs we have which take paramenters on command line and do some task. Like Del command of DOS takes name of a directory or file to delete that. If we want to create our own command line programs which take parameters to perform some task, we can do as follows:

#include <iostream.h>
#include <conio.h>

void main ( int argc , char *args[] )
{

cout << " Number Of Arguments : " << argc << endl ;
for ( int i=0 ; i<argc ; i++ )
{
cout << "arg["<<i<<"]: "<<args[i] << endl ;
}

}

How this works, we know that the main() method is the gateway to a C/C++ program, as this is a method, and methods can have parameters. We difined a main method with parameters.

No comments:

Post a Comment