Reading Command Line Options

You might think that the command line is a relic of the past. For a mortal user, that’s correct: The only people I know who still dwell at the command prompt are power users. I keep a command prompt (terminal) window open on my computers, just because using the terminal is fast and I happen to know the commands. Yet, internally, all graphical operating systems still reference the command line.

In fact, knowing a few command line tricks is one of those treasured “Windows secrets” you might read about. That’s because even Windows programs use command line options and arguments. So if your dream is to become a Windows programmer, you will eventually have to deal with the main() function’s twin arguments, argc and *argv[].

argc is the argument count

argv are the argument values, strings

Yes, *argv[] can be written as **argv. To do so is pure evil.

The **argv variable references a string of pointers. Each pointer holds a single argument specified at the command prompt that ran the program. Even Windows programs use those arguments, so this variable isn’t a quaint antique from the days when computers ran only in text-mode.

While you don’t see a command prompt when a Windows program launches, the system() or GUI equivalent command is fully capable of specifying command line arguments.

The argc variable is an int value that describes how many **argv pointers are available. Even when argc is zero, the **argv pointer still references a single item: the name of the program or command being run.

#include <stdio.h>

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

    for(x=0;x<argc;x++)
        printf("Argument %d: %s\n",x,*(argv+x));

    return(0);
}

The main() function declares its arguments at Line 3. A for loop at Line 7 inches through each of the arguments; even when argc equals zero, the loop spins once, displaying the argument zero, which is the program’s name.

The *(argv+x) construction in Line 8 uses variable x to offset each of the pointers referenced by argv. Here’s a sample run:

blog$ ./a.out roses are red "violets are blue"
Argument 0: ./a.out
Argument 1: roses
Argument 2: are
Argument 3: red
Argument 4: violets are blue

Above, the program name is a.out. See how the argument enclosed in double quotes is treated as one item? Figure 1 illustrates Code::Block’s output on a Windows PC:

Figure 1. Output to a DOS window for Code::Blocks. (Click to embiggen.)

Figure 1. Output to a DOS window for Code::Blocks. (Click to embiggen.)

In Code::Blocks, you must specify the command line arguments by using the Project>Set programs’ arguments command. Type the arguments into the Select Target dialog box, shown in Figure 2.

Figure 2. Setting command line arguments in Code::Blocks.

Figure 2. Setting command line arguments in Code::Blocks.

Yes, I know that the apostrophe after programs is improper English. I suppose some programmers have a “stick the apostrophe anywhere” attitude about English like other programmers have a “stick the asterisk anywhere” attitude about pointers.

This code could have also been written by using array notation, which — honestly — is how most C programmers would write it:

#include <stdio.h>

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

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

    return(0);
}

You’ll find one other location where the ** type of variable is encountered: the computer’s environment. My Lesson from April 2013 briefly covered how it works.

Leave a Reply