Wednesday, 28 November 2018

Command Line Arguments in C Language


Parameters or Values can be passed to program from the command line which are received and processed in main function. Since the arguments are passed from the command line hence they are called as command line arguments. All commands on Unix Operating System use this concept.

Two built in formal parameters are used to accept parameters in main.

  argc : Contains number of command line arguments. It is type int.

  argv : A pointer to an array of strings where each string represents a token of the arguments passed. It is a character array of pointers.

  main(int argc, char *argv[])



Example:

     #include <stdio.h>
int main( int argc, char *argv[] )
{
   if( argc == 2 )
   { 
       printf("The argument supplied is %s\n", argv[1]);
   }
   else if( argc > 2 )
   {
        printf("Too many arguments supplied.\n");
    }
   else
   {
         printf("One argument expected.\n");
    }
}

For Complete C Language Training in Gwalior

No comments:

Post a Comment