C Tutorial -- Command Line Arguments

When executing the program, you can pass values to the C program from the command line. These values are called command line arguments…

C


What is Command Line Arguments

​ When executing the program, you can pass values to the C program from the command line. These values are called command line arguments, and they are important to the program, especially when you want to control the program from the outside, rather than hard-coding these values in the code. Even all the program in Linux will provide command line arguments.

Use Command Line Arguments

​ Command line arguments are handled using main() function arguments, where argc refers to the number of arguments passed in, and argv[] is an pointer array which point to each argument passed to the program. The following is a simple example:

#include <stdio.h>

int main( int argc, char *argv[] )  
{
   if( argc == 2 )
   {
      printf("The argument supplied is %s\n", argv[1]);
   }
   else if( argc > 2 )
   {
      printf("Too many arguments.\n");
   }
   else
   {
      printf("No argument.\n");
   }
}

​ It produces the following result:

  • No argument
$./a.out
No argument.
  • Single argument
$./a.out aaarrrggg
The argument supplied is aaarrrggg
  • Two arguments & Above
$./a.out arg1 arg2
Too many arguments.

Explanation — argv[0], argv[1]

​ You should know that argv[0] holds the name of the program itself and argv[1] is a pointer to the first command line argument supplied, and *argv[n] is the last argument. If no arguments are supplied, argc will be 1, and if you pass one argument then argc is set at 2.

​ You pass all the command line arguments separated by a space, but if argument itself has a space then you can pass such arguments by putting them inside double quotes " " or single quotes ' '. So, let’s try this:

$./a.out "arg1 arg2"
The argument supplied is aaarrrggg

getopt() & getopt_long()

​ In Linux, we can use getopt() and getopt_long() to pass arguments:

#include<stdio.h>

int main(int argc, char *argv[])
{
    char *optstr = "p:n:m:c:";
    struct option opts[] = {
        {"path", 1, NULL, 'p'},
        {"name", 1, NULL, 'n'},
        {"mtime", 1, NULL, 'm'},
        {"ctime", 1, NULL, 'c'},
        {0, 0, 0, 0},
    };
    int opt;
    while((opt = getopt_long(argc, argv, optstr, opts, NULL)) != -1){
        switch(opt) {
            case 'p':
                strcpy(path, optarg);
                break;
            case 'n':
                strcpy(targetname, optarg);
                break;
            case 'm':
                modifiedtime = atoi(optarg);
                break;
            case 'c':
                changetime = atoi(optarg);
                break;
            default:
                if(strchr(optstr, optopt) == NULL){
                    fprintf(stderr, "unknown option '-%c'\n", optopt);
                }else{
                    fprintf(stderr, "option requires an argument '-%c'\n", optopt);
                }
                return 1;
        }
    }
    findInDir(path);
    return 0;
}

© 2020-2021. All rights reserved.