Command Line Arguments in C

1.What are command line arguments in C?

C provides us a method to pass parameters to main function. Instead of specifying main() with no parametes, we can specify
 int main(int argc, char *argv[])
 
Here argc is the argument count. It gives the number of command line arguments. argv is an array of character pointers. argv holds the command line arguments.

e.g. If we execute a program using the command
 myprogram 10 20 30
 

argc will be 4. And argv[0] will be myprogram. argv[1] will be string 10. argv[2] and argv[3] will be strings 20 and 30 respectively.

argv[0] is always the command name. Which means argc must be at least 1.

Command line arguments provides an efficient way to pass parameters to programs. Let us say, you write a program to copy a file to another file. Instead of reading these file names using input operation each time, we can provide these two as command line arguments and provide the file names when executing the program.

On some systems, you can even use a third parameter char *envp[] which is an array of character pointers and stores the envirnoment variables. The syntax for this is

int main(int argc, char *argv[],char *envp[])
2. Write a C program to find the largest of command line arguments.

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
    int i,largest=0;
    if(argc<=1)
       printf("No numbers provided");
    else
   {
        largest = atoi(argv[1]);/*argv[0] is command name*/
       for(i=2;i<argc;i++)
       {
             int n = atoi(argv[i]);
             if(n>largest)
                 largest = n;
       }
      printf("Largest number is %d\n",largest);
   }
  return 0;
}
3. Write a program to read command line arguments and find the sum and product of them. If no arguments are given the program should print that.

#include<stdio.h>
#include<stdlib,h>
int main(int argc,char *argv[])
{
   int arr[20],i,len,sum=0,prod=1;
   if(argc<2)
     {
        printf("No command line arguments\n");
        return 0;
      }
   len = argc-1; //0th element is program name
   for(i=0;i<len;i++)
   {
       int temp = atoi(argv[i+1]);
       sum+=temp;
       prod*=temp;
   }
   printf("Sum is %d and product is %d\n",sum,prod);
   return 0;
}
4. Write a program to print gcd of first two command line arguments provided. Calculate gcd using recursive function.

#include<stdio.h>
#include<stdlib.h>
int gcd(int a,int b);
int main(int argc,char *argv[])
{
   int a,b,answer;
   if(argc<3)
     {
        printf("Please provide two command line arguments..\n");
        return 0;
      }

    a = atoi(argv[1]);
    b = atoi(argv[2]);
    if(b>a)
    {
          int t = a;
          a = b;
          b = t;
     }
    answer = gcd(a,b);
    printf("Greatest common divisor of %d and  %d is %d\n",a,b,answer);
    return 0;
}

int gcd(int a,int b)
{
   if(a%b >0)
       return gcd(b,a%b);
   else
       return b;
}
5. What will be the output of the program (myprog.c) given below if it is executed from the command line?
 myprog one two three
 /* myprog.c */
#include<stdio.h>
 int main(int argc, char **argv)
 {
    printf("%c\n", **++argv);
   return 0;
 }

Output : o

++argv increments the argument vector and points it to the 1st command line argument which is one. ** gives us the character o.
6. Write a program to implement "cat". The program should accept text file name as command line argument and print the content of the file.

#include<stdio.h>
void print_file(FILE *fptr)
{
    int ch;
    while ( (ch=fgetc(fptr))!=EOF)
       putchar(ch);
}
int main(int argc,char **argv)
{
   FILE *fptr;
   if(argc<2)
   {
       printf("filename is not given at command line");
       return 0;
   }
   fptr = fopen(argv[1],"r");
   if(fptr==NULL)
   {
      printf("Can not open %s\n",argv[1]);
      return 0;
    }
    print_file(fptr);
    fclose(fptr);
    return 0; 
} 
7. Write a program to accept a number as command line argument and print its cube.

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
   int n;
   if(argc<2)
   {
     printf("Please supply a number as command line argument\n");
     exit(1);
   }
   n = atoi(argv[1]);
   printf("The cube of %d is %d\n",n,n*n*n);
   return 0;
}
8. Write a program to print all command line arguments.

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