Retrieve Command-Line Arguments in C++

  1. Use int main(int argc, char *argv[]) Notation to Retrieve Command-Line Arguments in C++
  2. Use int main(int argc, char **argv) Notation to Retrieve Command-Line Arguments in C++
  3. Use the while Loop to Print Out Command-Line Arguments in C++

This article will explain several methods of how to retrieve command-line arguments in C++.

Use int main(int argc, char *argv[]) Notation to Retrieve Command-Line Arguments in C++

Command-line arguments are string values that can be passed to the program at its start of execution. Usually, these arguments are widely utilized by the programs that have CLI-only interface e.g. Unix command-line utilities. Note that, C++ program should have a special main function definition to access command-line arguments. Namely, the main function should include the parameters as - (int argc, char *argv[]). argc parameter represents the total count of the arguments that were passed to the program, including the program name itself. On the other hand, argv stores the array of the character strings that denote the argument values, which can be accessed using the array notation - argv[0]. Mind that, the array of char pointers is guaranteed to be terminated with 0.

The following example demonstrates the for loop’s basic usage to print each argument value to the console. Note though, the preceding if statement makes sure the program has arguments specified and then continues the execution.

#include <iostream>
#include <string>

using std::cout;
using std::cerr;
using std::endl;
using std::string;

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

    if (argc < 2) {
        cerr << "No program parameters provided !" << endl;
        exit(EXIT_FAILURE);
    }

    for (int i = 0; i < argc; i++)
        printf("argv[%d] = %s\n", i, argv[i]);

    exit(EXIT_SUCCESS);
}

Use int main(int argc, char **argv) Notation to Retrieve Command-Line Arguments in C++

There’s another main function prototype that is valid and behaves exactly like the previous one, so we include it to make sure there is no confusion when the following notation occurs - int main(int argc, char **argv). Notice that accessing the elements is done in the same way, and they can be traversed using the for loop.

#include <iostream>
#include <string>

using std::cout;
using std::cerr;
using std::endl;
using std::string;

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

    if (argc < 2) {
        cerr << "No program parameters provided !" << endl;
        exit(EXIT_FAILURE);
    }

    for (int i = 0; i < argc; i++)
        printf("argv[%d] = %s\n", i, argv[i]);

    exit(EXIT_SUCCESS);
}

Use the while Loop to Print Out Command-Line Arguments in C++

Another method to access the argument values is to dereference the argv pointer until it equals zero. Note that, the argv array is guaranteed to be terminated with zero value, which we can utilize to implement a while loop to traverse the array and print the argument values one-by-one. Notice that the printf call in the if statement below acts as an error reporter that enough arguments were not passed to the program. The interesting part is that the program name is retrieved with the argv[0] notation and %s specifier, making it a more flexible method than hard-coding the name each time it changes.

#include <iostream>
#include <string>

using std::cout;
using std::cerr;
using std::endl;
using std::string;

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

    if (argc < 4) {
        printf("Usage: ./%s arg1 arg2 arg3\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    while (*argv) {
        cout << *argv << endl;
        argv += 1;
    }

    exit(EXIT_SUCCESS);
}
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.