Tuesday, May 13, 2008

Command Line Arguments

Command Line Arguments

  • An executable file of C is called as a command
  • We may pass arguments to this command while calling it for execution from the operating system environment Such arguments are known as commandline arguments
  • When a command is executed the function main() is called . The arguments are passed to main( ) function
  • The function main( ) takes two arguments

First argument is an int

Second argument is an array of char pointers

Prototype of main( ) function is :-

int main(int argc , char *argv[ ] ) ;

or

void main(int argc , char *argv[] );

int argc : number of arguments passed .This includes the command itself .

char *argv[ ] : Each pointer points to the starting address of each argument . argv[0] holds the address of the command itsef

Note:

All arguments and the command itself are treated as strings These arguments are passed at the operating system level.

Steps are :-

Write the source code (.c file)

Compile and Generate an exe file (.exe file)

Call this exe file at the command prompt by passing appropriate argument values .

For e.g

Write a program that counts down a number passed to it as a command line argument

Source file name : countdn.c

Executable file name countdn.exe

Command countdn

Case 1 : Suppose you run the program as

C:>countdn 5

Your output should be

Counting Down …. 5 4 3 2 1 0 Off !!

Case 2 : Suppose you run the program as

C:>countdn 10

Your output should be

Counting Down …. 10 9 8 7 6 5 4 3 2 1 0 Off !!

Case 3 : Suppose you run the program as

C:>countdn

Your output should be Nothing to Count Down !!

Case 4 : Suppose you run the program as

C:>countdn “asd”

Your output should be

Error : Non Numeric argument passed

Usage : countdn number

Case 5 : Suppose you run the program as

C:>countdn 2 4 5

Your output should be

Error : Invalid number of arguments passed

Usage : countdn number

Step1 :

Type and save the following source code with the name countdn.c

int main(int argc , char *argv[])

{

int j ;

clrscr( );

if ( argc == 1 ) /* case 3 No value passed */

{

printf(“\aNothing to Count Down !!”);

return 1 ;

}

if ( argc > 2 ) /* case 5 More than one value passed

{

printf(“\aError : Invalid number of arguments passed \n Usage : countdn number”);

return 2 ;

}

j = atoi( argv[1] ); /* convert “5” to number 5 */

if( j == 0 ) /* case 4 : A non numeric string was passed */

{

printf(“\a\nError : Non numeric argument passed \n Usage : countdn number”);

return 3 ;

}

printf(“Counting Down…….”);

while(j >= 0 )

{

printf(“%5d” , j);

--j ;

}

printf(“ Off !! “);

return 0 ;

}

Step 2 :

Compile and generate countdn.exe . The command is countdn

Step3 :

Run the command at Command prompt as :

C:> countdn 10

Program 2 :-

Write a program greet that greets the username passed as commandline argument depending on the time of the day

# include

# include

int main(int argc , char *argv[])

{

struct time t;

int j ;

char str[20];

gettime(&t);

if( t.ti_hour <>

strcpy(str , "Good morning" ) ;

else

if( t.ti_hour >= 12 && t.ti_hour <= 16 )

strcpy(str , "Good Afternoon" );

else

if( t.ti_hour > 16 && t.ti_hour <>

strcpy(str , "Good Evening" );

else

strcpy(str , "Good Night");

if(argc == 1 )

{

printf("%s The whole world" , str );

return 1 ;

}

for(j = 1 ; j <>

printf("\n%s %s" , str , argv[j] );

return 0;

}

No comments: