Parsing C++ Command-Line Arguments

Microsoft Specific

Microsoft C/C++ startup code uses the following rules when interpreting arguments given on the operating system command line:

The following program demonstrates how command-line arguments are passed:

include <iostream.h>

void main( int argc,      // Number of strings in array argv
          char *argv[],   // Array of command-line argument strings
          char *envp[] )  // Array of environment variable strings
{
    int count;

    // Display each command-line argument.
    cout << "\nCommand-line arguments:\n";
    for( count = 0; count < argc; count++ )
         cout << "  argv[" << count << "]   "
                << argv[count] << "\n";
}

Table 2.2 shows example input and expected output, demonstrating the rules in the preceding list.

Table 2.2

Command-Line Input argv[1] argv[2] argv[3]
"abc" d e
abc
d
e
a\\\b d"e f"g h
a\\\b
de fg
h
a\\\"b c d
a\"b
c
d
a\\\\"b c" d e
a\\b c
d
e

END Microsoft Specific