C Commandline Arguments

Command line arguments are the parameters, which are supplied to the program when it is invoked. These command line arguments are specified after the program name in operating systems command prompt or command shell, and these arguments values are passed to the program at the time of execution by the operating system. The example of program with command line arguments is :
 $ <program_name> <argument1> <argument2>..... <argumentN>
Example :
 $ ./myprogram arg1 arg2
The command line  arguments are mostly used when the user wants to control the program from outside instead of hard coding the input values inside the code.

Command line argument is an important concept in C programming. In C programming, the command line arguments are handled using main() function arguments. The syntax for main function to handle command line argument is :
 int main(int argc, char *argv[])
Where argc (Argument Count) is an integer variable, which holds or counts the number of arguments on the command line and argv[] (Argument Counter) is a pointer array of type char, which points to the arguments passed to the program. Now lets take a look at C example code.
#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("Number of arguments are : %d\n", argc);
  printf("First Argument argv[0] : %s\n", argv[0]);
  printf("First Argument argv[0] : %s\n", argv[1]);
  printf("First Argument argv[0] : %s\n", argv[2]);
  return 0;
}
Now when we run the above program, with two arguments then the output will  be :

$ ./a.out arg1 arg2

Number of arguments are : 3
First Argument argv[0] : ./a.out
First Argument argv[1] : arg1
First Argument argv[2] : arg2

Now as we can see that :
  • argc will give the total number of arguments, which is at above example : 4. (including the program name itself.)
  • argv[] holds the arguments values in a character array, which can be accessed by index or subscripts. Also note that the argv[0] holds the program name itself, and the rest of the arguments are accessed by :
 argv[1], argv[2], argv[3]....
We can also write the above example as :
#include <stdio.h>

int main(int argc, char *argv[]) {
  int c;
  printf("Number of arguments are : %d\n", argc);

  for(c=0;c<argc;c++) {
    printf("First Argument argv[%d] : %s\n", c, argv[c]);
  }

  return 0;
}
Output :

$ ./a.out arg1 arg2 arg3 arg4

Number of arguments are : 5
First Argument argv[0] : ./a.out
First Argument argv[1] : arg1
First Argument argv[2] : arg2
First Argument argv[3] : arg3
First Argument argv[4] : arg4

Also note that if you want to supply the argument with spaces, then you have to use the parenthesis with the argument.
$ ./program  "Argument with spaces"
For example the below C program will print the given argument on the screen.
#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("%s\n", argv[1]);
  return 0;
}
Output :

./a.out "Hello world, This is Test"
Hello world, This is Test

At above example if we do not provide any argument to the program, then it will crash. So we can check and remove this error by using some if conditions. For example :
#include <stdio.h>

int main(int argc, char *argv[]) {
  if(argc != 2) {
    printf("Error : Insufficient Arguments ??.\n");
    printf("Usage : %s \"Progran Argument\"\n", argv[0]);
    return 1;
  }

  printf("%s\n", argv[1]);
  return 0;
}
Output :

$ ./a.out
Error : Insufficient Arguments ??.
Usage : ./a.out "Progran Argument"

$ ./a.out "This is Test String"
This is Test String

At above example, if we run the program without any arguments then it prints the error message and exits with return value 1, otherwise it prints the arguments.

Below is another example of using command line arguments in C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int add(int a, int b) {
  return a+b;
}

int mul(int a, int b) {
  return a*b;
}

int main(int argc, char *argv[]) {
  int num1, num2;
  if(argc != 4) {
    printf("Insufficient Arguments.\n");
    printf("Usage : %s add/mul <num1> <num2>\n", argv[0]);
    return 1;
  }

  num1 = atoi(argv[2]);
  num2 = atoi(argv[3]);

  if(strcmp(argv[1], "mul")==0) {
    printf("%d x %d : %d\n", num1, num2, mul(num1, num2));
  } else if(strcmp(argv[1], "add")==0) {
    printf("%d + %d : %d\n", num1, num2, add(num1, num2));
  } else {
    printf("Error in 2nd argument : %s\n", argv[1]);
    printf("Please type it correctly.\n");
  }
  return 0;
}
Output :

$ ./a.out

Insufficient Arguments.
Usage : ./a.out

$ ./a.out mul 12 15

12 x 15 : 180

$ ./a.out add 12 15

12 + 15 : 27

Next Topic :