Command line arguments in C

What are the different ways of providing input in a c programme?

You have used scanf, gets, getc. There is another much easier way. That is to provide the values along with program name when executing it.

So instead of saying
./a.out  you would use
./a.out hello world

When such arguments are provided they have to be accessed using main function with an int parameter and another array of char pointers.
These are  called argc (argument count) and argv (argument vector) respectively.


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

Let us start with a simple program. 

#include<stdio.h>
int main(int argc,char *argv[])
{
   printf("argc is %d\n",argc);
   printf("and argv[0] is %s\n",argv[0]);
   return 0;
}

If we compile and execute this program, the output will be

yourhomedir$./a.out
argc is 1
argv[0] is ./a.out 

 Let us understand the program. As we are running the program with no extra argument, argument count is 1 and 0th element of argument vector is executable name itself.

 If we run the program again with hello world, argc will be 3

yourhomedir$./a.out hello world
argc is 3
argv[0] is ./a.out 

Let us use these arguments and write a program to print all the command line arguments

#include<stdio.h>
int main(int argc,char *argv[])
{
   int i;
   printf("argc is %d\n",argc);
   for (i=0;i<argc;i++)
    printf("and argv[%d] is %s\n",i,argv[i]);
   return 0;
}

This is similar to previous program, except that we have added a loop to print all elements of argv in that loop.  And the loop repeats argc number of times.

Let us compile and execute this.

yourhomedir$gcc b1.c 
yourhomedir$./a.out hello beautiful world
 argc is 4
argv[0] is ./a.out
argv[1] is hello
argv[2] is beautiful
argv[3] is world


You can write a program to add the numbers provided by command line arguments. You can store these numbers in an array and find the largest of all and so on. 

But we will finally write a program to print a file whose name is given as command line argument.

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
   if(argc<2)
   {
 printf("Syntax is ./a.out filename");
        return 0;
   }
   FILE *fptr = fopen(argv[1],"r");
   if(fptr==NULL)
      {
         printf("File open error");
         exit(1);
      }
   char *str;
   while( (str=fgets( str,100,fptr))!=NULL)
      printf("%s",str);
   fclose(fptr);
}

yourhomedir$gcc b2.c 
yourhomedir$./a.out b2.c
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
   if(argc<2)
   {
    printf("Syntax is ./a.out filename");
        return 0;
   }
   FILE *fptr = fopen(argv[1],"r");
   if(fptr==NULL)
      {
         printf("File open error");
         exit(1);
      }
   char *str;
   while( (str=fgets( str,100,fptr))!=NULL)
      printf("%s",str);
   fclose(fptr);
}


So what we are doing here is we take the 1st element of argv as filename and try to open that file. If file open is successful, we read one line at a time and display it on screen.  

Quite simple. Isn't it?

For such programs and quiz on these topics, please download my app - C interview questions
 

Comments

Popular Posts