Command-line parameters are one commonly-used and convenient mechanism for getting information from the "outside world" into a C++ program at run time. Here are some things you need to note and remember about command-line parameters:

  1. From a user standpoint, the user enters, at the command-line prompt, something like
    prompt> myprogram one two 5 17 *!!
    
    in which the program to be run is myprogram, and each of the items after it (one, two, 5, 7 and *!!) is a command-line parameter for this program. What the program decides to do with this information is its business, but it's very important to remember three things:
    1. There can be as many of these command-line parameters as you like.
    2. Each command-line parameter value is interpreted as an old-fashioned C-string. Thus, even command-line parameters that "look like" numbers (17, for example) are read in as C-string values and must be converted to numbers if the program is to use these values in actions that only numbers can be involved in, like arithmetic.
    3. Whitespace is used to delimit command-line parameters, so if a single command-line parameter actually contains whitespace, the user must place the entire parameter within double quotes, as in
      prompt> setupdate "John Doe" "Mary Roe"
      
      where the program is supplied with only two command-line parameters.
  2. From a programmer standpoint, to make use of command-line parameters, you must first make sure your main function has the two parameters illustrated in
    int main(int argc, char* argv[])
    
    in which the names argc and argv are not required, but are very traditional and should be used. For one thing, using these names is mnemonically helpful, since
    1. You can think of argc as being an abbreviation for argument count, the number of command-line arguments. However, be careful because the name of the program itself is actually the first command-line parameter, so the value of argc is in fact 1 + the number of command-line parameters actually entered by the user.
    2. You can think of argv as being an abbreviation for argument values, since it is the name of an array of pointers to C-string values (character pointers). As mentioned above, the first value is the name of the program itself. So, argv[0] points to the name of the program, argv[1] points to the first parameter entered by the user, argv[2] points to the second parameter entered by the user, and so on. Since the user can enter as many parameters as desired, argv is clearly a "dynamic array" whose storage allocation is handled "behind the scenes".
  3. An alternate form of the parameter list in the main function is
    int main(int argc, char** argv)
    
    which looks scarier but is actually equivalent to the previous form. I mention it only because you may see it, but I would recommend use of the first form mentioned, since it is arguably somewhat more intuitive and readable.
  4. Programs that use command-line parameters typically begin by checking to see if the right number and/or kinds of values have been entered before proceeding to process those entries.
  5. A final (redundant) reminder: argv[i] is a pointer to the ith item entered by the user on the command-line, after the name of the program, and is considered to be a C-string. Whatever the program needs to do with this value needs to take this into account.