Skip to Content

C/C++: Command Line Arguments

When beginning programmers think of getting input into a program from the user, they usually think about prompting the user for input and reading what they type in.

But for small amounts of data, command line arguments are a much better solution. These are the extra words (stuff separated by spaces) that you type on the command line after the name of the program you want to run.

For example, when you type “gcc -o myprog myprog.c”, you are using command line arguments. Everything after gcc are arguments that are passed to the program gcc. (The same is true for g++).

Command line arguments in C/C++ are passed as parameters to main. The official full definition of main is:

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

The first parameter to main, argc, is the count of the number of command line arguments. Actually, it is one more than the number of arguments, because the first command line argument is the program name itself! In other words, in the gcc example above, the first argument is “gcc”.

The second parameter is an array of character pointers. In C, a character pointer is usually pointing at a C string; the pointer contains the address of the first character in the string, and then the rest of the characters in the string occupy memory sequentially at increasing addresses. A C string must always end with a null character, which is a character value of zero. The character ‘\0’ is this value, but people often just use 0, too.

The argv array contains exactly argc number of entries. Since C arrays start at index 0, the last valid entry is at (argc-1). Each entry is a valid C string. All arguments are strings, even if they are numeric digits. If you need a number, it is your responsibility to convert the string argument into a number.

Try copying this C program into a file, compiling and running it:

#include <stdio.h>

int main(int argc, char *argv[])
{
    int i;
    printf("number of arguments: %d\n", argc);
    for (i=0; i < argc; i++) {
        printf("argument %d: (%s)\n", i, argv[i]);
    }
    return 0;
}

For a basic C++ program that accesses arguments, try this one:

#include <iostream>
#include <string>
#include <vector>

int main(int argc, char *argv[])
{
    int i=0;
    auto args = std::vector<std::string>(argv, argv+argc);
    std::cout << "number of arguments: " << argc << "\n";
    for (std::string arg: args) {
        std::cout << "argument " << i++ << " (" << arg << ")\n";
    }
    return 0;
}

When you run this program, try it with no arguments, 1 argument, and many arguments. Try it with arguments in single and double quotes with spaces in them. Try it with “ > outputfile” on the command line, which we know redirects the output to the file. Then look at what is in the file.

Command line arguments are really that simple. Just remember that they are all strings. If you need to convert any to numbers (integer or real), then look at converting strings to numbers.

Other resources

https://www.cprogramming.com/

https://www.learn-c.org/