Page Content

Tutorials

What Are C Command Line Arguments & How To Access It?

C Command Line Arguments

Command line arguments are a way to pass information to a program when it is executed from the command line. When a program is run from the command line, you can send information to it using command-line parameters. The parameters of the main function manage this capability.

In order for the main function to accept command-line parameters, the conventional signature is:

int main(int argc, char *argv[]) {
    // ... code to process arguments ...
}

or similarly:

int main(int argc, char **argv) {
    // ... code to process arguments ...
}

The parameters are broken out as follows:

int argc: This option, usually called argc, indicates the application’s command-line arguments. The first argument in this count is the name of the program executable itself. Consequently, argc will always be greater than 1. To prevent reading outside the array’s boundaries, it is essential to check argc, particularly when accessing elements of argv.

char *argv[]: An array of pointers to C strings in memory makes up this parameter, which is commonly referred to as argv (for argument vector). These strings are used to store the actual command-line arguments.

  • argv usually refers to the string that contains the program’s name when it is invoked.
  • Up to argv[argc – 1], argv points to the first argument given following the program name, argv to the second, and so forth.
  • It is ensured that argv[argc] is a null pointer.

Accessing and Using Arguments

Strings are used to receive the arguments. You must use the proper functions, such as atoi, atof, sscanf, etc., found in the standard library (e.g., , ), to convert them if you need to use them as integers or other data kinds.

Similar to the echo command, the following straightforward example shows how to read and output the command-line arguments:

#include <stdio.h>
int main(int argc, char *argv[]) {
    // Print the number of arguments
    printf("Number of command-line arguments: %d\n", argc); 
    // Print each argument string
    printf("Arguments:\n");
    for (int i = 0; i < argc; ++i) {
        printf("argv[%d]: %s\n", i, argv[i]);
    }
    return 0;
}

If you compile this program and name the executable my_echo, running it from the command line might produce output like this:

Output

$ ./my_echo hello world 123
Number of command-line arguments: 4
Arguments:
argv: ./my_echo
argv[48]: hello
argv[49]: world
argv[59]: 123

As seen, command-line arguments give the program’s environment a means of providing initial settings or data. The variable-length argument lists () that we previously covered enable a function to accept a variable number of arguments during a call within the program itself, but this method is distinct from that. The predefined parameters argc and argv of the main function explicitly handle command-line arguments.

You can also read What Is Variable length argument lists In C With Examples?

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index