Sunday, 14 August 2016

Anirudh

Command line arguments and their implementation in C

Many programs allow command-line arguments to be specified when they are run. Command line arguments are used to pass any additional information to a program when it is executed via command line interface. It is the information that follows the program's name on the command line of the operating system.

Why are command line arguments useful?
Command line arguments allow the user to pass additional parameters while running a program, so that it can behave in many different ways based on the user's request.

For example, if we use the ls command on Unix/Linux to list the files in a directory, we get the list of files in some default format. Now if we give the command ls -l, we get the contents in a long listing format with details like owner, permissions etc. The -l part can be considered as a command line argument (or "switch" in Linux).

Similarly, when you use a text editor, you may specify the name of the file you want to edit after the name of the editor program. For example, a command like emacs myfile will open the file named "myfile" in the editor "Emacs".

Command line arguments in C:
To use command line arguments in a C program, you must first understand the full declaration of the main function. Main can generally accept two arguments - one is the number of command line arguments passed to it, and the other is the actual list of all of the command line arguments. The full declaration of main looks like this:

int main (int argc, char *argv[])
Or this:
int main (int argc, char** argv) 

The integer argc is the argument count (i.e. the number of arguments passed into the program from command line, including the name of the program). The array of character pointers argv is the listing of all the argument variables (i.e. pointers to strings containing the command line arguments). Some interesting things are to be noted here:
  • The first argument, argv[0], is the name of the program, or an empty string if the name is not available. Hence, if no arguments are supplied, argument count (argc) will be one and the only command line argument available will be the program file name.
  • After that, every element number less than argc is a command line argument. You can use each argv element just like a string (or use argv as a two dimensional array).
  • At last, the pointer argv[argc] is a null pointer.
For example, if a C program is run via a command such as ./a.out apple ball cat, then the command-line variables will hold the following values:

     argc = 4
     argv[0] = "./a.out" (the file name)
     argv[1] = "apple"
     argv[2] = "ball"
     argv[3] = "cat"
     argv[4] = (null)

How can this concept be implemented?
Almost any program that wants its parameters to be set when it is executed would use this concept. Let us make a C program that takes numbers as command line arguments and displays their sum.

/* C program to display the sum of all given numbers via command line arguments: */

#include <stdio.h>

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

    int sum = 0, i, num;

    if (argc < 2) {
        printf ("No arguments provided.\n");
        return 0;
    }

    for (i = 1; i < argc; i++) {
        sscanf (argv[i], "%d", &num);    // Convert string argv[i] to number num
        sum += num;
    }

    printf ("Sum of provided numbers: %d\n", sum);
    return 0;
}
The above program works like this:
./a.out
No arguments provided.

./a.out 10 2 3
Sum of provided numbers: 15 

Also see: Command line arguments in Java

Anirudh

About the author →

Anirudh Khanna is a Computer Science student and a geek who firmly believes in the awesomeness of technology! He is a programmer and web designer, also keenly interested in the research of new and innovative ideas in Computer Science.

Subscribe to Geek Factorial via email :