Friday, 5 January 2018

Command Line Arguments


Command Line Arguments in C Programming

        In C Programming , it is possible to pass arguments at the time of Execution. For doing so, the brackets after main are used. There are special two built-in arguments, argc and argv
The parameter argc is of Integer type, which will hold the number of arguments passed as argument to the command line. Its value is always at least 1, because the name of the program is treated as one of the arguments in the command line arguments. 
The  parameter argv is an character array of pointers to the command line arguments. Each element of the array points to a command line argument. i.e., argv is a pointer array which points to each argument which is passed to main. 
Let us see an example: 

#include<stdio.h>

main( int argc, char * argv[])
{
    if( argv == 2 )

         printf("The arguments passed is %s\n",argv[1]);

    else if ( argc > 2 )

         printf("More than one arguments passed. \n");

    else 

         printf("One argument expected.\n");

}

Note that *argv[0] is the name of the program which is invoked therefore *argv[1] is the first argument passed and hence *argv[n] is the last argument. If no argument is passed then argc will be one. To execute the program, the program is called by command line as,

       ProgramName   argument1  

Here only one argument is passed. To pass multiple arguments we can write it as, 

      ProgramName    argu1  argu2  argu3..... 

Execution of  C Program through Command Line Argument can be done on various compilers. It can be done in various ways, some are as follows,

Execution of C Program using Command Line Arguments in Turbo C/C++ . ( click here to see full post )
Sample Screenshot






Execution of C Program using Command Line Arguments in CodeBlocks . ( click here to see full post )

Sample Screenshot





For detailed instructions how to execute, click on to links above. 

No comments:

Post a Comment

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...