Command Line Arguments


Most systems that support C++ allow the user to specify values on the command line when the program is run, so that it is easy to alter certain run-time behaviors. Otherwise, you would have to alter and recompile the program, or resort to some other inconvenient way to provide this flexibility.

These values are supplied from the command line, after the program name. An example might be:

./myprog dog cat frog

Obviously, the user cannot just type anything. The user must know what valid values are, and the program must be coded to detect and respond to them.

When the program starts running, it can look at the user-supplied command line arguments and do one thing or another (a decision) based on their values.

(Note that this is different than I/O redirection, which uses the special < or > symbols and only allows a file to be specified.)

For example, you may want to control how many numbers per line are printed by a program. You might invoke the program as:

./myprog 8

or

./myprog 15

The program can get this value (as a C string) and set a program variable (as an integer) so that it can control the number of lines displayed.

How does a program get these values?

They are passed to the main() function as arguments.

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

These arguments are usually named as shown.

The first argument tells how many separate arguments were passed - it is the count of arguments.

The second argument represents an array of pointers to characters, but is perhaps better described as an array of C strings. A picture can help clarify this...

Command line arguments

The C strings in the array are the values passed: "10" or "dog" "cat" "frog"

The first (i.e., the 0th) argument is always the pathname used to execute the program: ./myprog

That means argc is always 1 or more because there is always at least the program pathname.

You could access and display these arguments with the following code:

for (int i = 0; i < argc; i++)
    cout << argv[i] << endl;

You could store the nth C string (if argc is at least n + 1) into another character array:

char s[80];
strcpy(s, argv[n]);

Example: Get a number from the command line to use to specify the number of values to calculate:

#include <string>
#include <cctype>
#include <cstdlib>

using std::cout;
using std::stoi;

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

    if (argc > 1)
    {
        // Make sure that argument string contains nothing but digits
        for (i = 0; argv[1][i] != '\0'; i++)
        {
            if (!isdigit(argv[1][i]))
            {
                cout << "Bad character in command line argument\n";
                exit(1);
            }
        }

        // Convert argument from C string to integer
        max_number = stoi(argv[1]);
    }
    else
    {
        cout << "Error: missing command line argument\n";
        exit(1);
    }

    // Now use max_number as an int in the rest of our code...
    ...

}