How to Retrieve Command-Line Arguments in C++

Jinku Hu Feb 02, 2024
  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++
How to Retrieve 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::cerr;
using std::cout;
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::cerr;
using std::cout;
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::cerr;
using std::cout;
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);
}
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook