Tuesday 25 December 2012

COMMAND LINE ARGUMENTS IN C

COMMAND LINE ARGUMENTS

  • Here the argument are passed to main functions through the command prompt and its very useful feature in c language 
  • the CLM consists of two main argument passing variables that are  INT  ARGC and CHAR **ARGV[]  , 
  • The argc shows the number of argument that has been passed to the main function through command prompt. and it will be in the integer data type .
  • The character array argv[] has been used to store the arguments . and here the argv[0] will consists of program name and agrv[1],argv[2]...will consists of argument values.  

        the main syntax of command line argument is given below

                          #include <headerfile.h>

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



   =>   let us see an example for command line argument .
        Addition of two numbers

           1.In TC   c:/tc/bin
            program name : add.c

          
              #include<stdio.h>
              #include<conio.h>
              void main(int argc,char  **argv[])
              {
                      int a,b,c;
                     a=atoi(argv[1]);

                     b=atoi(argv[2]);   //atoi can be used to covert character to integer
                  
                      if(argc!=3)
                       {
                          printf("invalid input");
                          exit(0);
                        }
                     
                      c=a+b;
                    printf("c= %d",c);
              }




     PROGRAM AND OUTPUT WINDOW:

                   
                          

0 comments:

Post a Comment

Search Here...