Hi guys, In this document we are going to see how we can use command line arguments
in C programs . You may have see that when we use tools like Nmap , SQLmap, WPscan etc we supply arguments to proram while we launching it. For an example if we want to ping a host with "ping" utility we use the command "ping hacksland.net"
So hear hacksland.net is an argument to the program. But, why we use command line arguments while we can use keybord inputs in our programs?.

So in C language we have two variables to deal with CL arguments. Those are argv and argc. The argc is an integer variable and it holds the number of arguments given from user. argv is a pointer to a character array. This array holds all arguments provided by user. Actually there is an interesting point hear. argv is nt only holding arguments iven by user. It's first value is the program name. That means if we acces argv[0] we can get the program name. So argc is equel to number of provided arguments + 1 . So argc is always greater than or equel 1. It'll be 1 if there are no arguments.

Let's write a simple proram to demonstrate the usage of command line arguments in C.

#include <stdio.h>

int main(int argc, char const *argv[])
{
  printf("argv[0] holds (Program name) : %s\n", argv[0]);
  printf("argv[1] holds (first argument) : %s\n", argv[1]);
  printf("argv[2] holds (Second argument) : %s\n", argv[2]);

return 0;
}

 

As the first step I declared main function with two arguments. (int argc and char const *argv[])

Let's compile and run it. Hear I gave two arguments as "Python" and "Java"

thilan@bt:~/programming/c$ ./cli-input Python Java
argv[0] holds (Program name) : ./cli-input
argv[1] holds (first argument) : Python
argv[2] holds (Second argument) : Java

Now we have to think about the stability of our programm when we et user inputs. What if we expect user to input command line arguments and user dosen't give them? Hear we have an example run of above program without giving arguments.

thilan@bt:~/programming/c$ ./cli-input
argv[0] holds (Program name) : ./cli-input
argv[1] holds (first argument) : (null)
argv[2] holds (Second argument) : LS_COLORS=di=34:ln=35:ex=31

Hear you can see we are getting some unexpected results. If we use this with a real program it will cause to a program crash. So it is safe to check user has provided arguments or not before acces them. Following program has a checking feature.

#include <stdio.h>

int main(int argc, char const *argv[])
{
  if (argc >= 3)
  {
    printf("argv[1] holds (first argument) : %s\n", argv[1]);
    printf("argv[2] holds (Second argument) : %s\n", argv[2]);
  }else{
    printf("[ERROR] You haven't supply requred arguments\n");
  }
return 0;
}

 

Let's run it again with no arguments and see what happen.

thilan@bt:~/programming/c$ ./cli-input
[ERROR] You haven't supply requred arguments

This time program coud manage the situation and stops next execution.

Now I have to tell another point to you. All of arguments given by user will be saved as character strings. So even user enter an integer as command line argument it will treated as string. So what if you want to get integer input from user? Check following code. I'm tryin to get a number from user and add another number(5) to it.

#include <stdio.h>

int main(int argc, char const *argv[])
{
  if (argc < 2)
  {
    printf("[ERROR] Input a number as argument\n");
  
}else{ int a = argv[1]; int b = a + 5; printf("Your input number is : %d\n", a); printf("input number + 5 equal to : %d\n", b); } return 0; }

 

Let's compile it to see what happen.

thilan@bt:~/programming/c$ gcc cli-input.c -o cli-input
cli-input.c: In function ‘main’:
cli-input.c:11:11: warning: initialization of ‘int’ from ‘const char *’ makes integer from pointer without a cast [-Wint-conversion]
int a = argv[1];
^~~~


Ohh, we got an error. But why? The input from user is a string. But we are trying to add an integer to it. This is not correct accordion to c syntax. One thin we can do is use Casting.
Also we can convert user input to an integer before use it. We can use atoi() function for this purpose. The function name atoi stands for ASCII to Integer.This will convert a ASCII character string into a number. Before we use atoi function we have to include a library (Or header file) called stdlib.h. Following is the corrected C code for above program.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
  if (argc < 2)
  {
    printf("[ERROR] Input a number as argument\n");
  }else{
    int a = atoi(argv[1]);
    int b = a + 5;

    printf("Your input number is : %d\n", a);
    printf("input number + 5 equal to : %d\n", b);
  }
  return 0;
}

Let's compile and run it again.

thilan@bt:~/programming/c$ ./cli-input 4
Your input number is : 4
input number + 5 equal to : 9

So guys that's all for this tutorial. I'll write more on this topic. Thank you for reading.