previous | start | next

## Command Line Arguments

- Starting a program by typing the name of the program at a prompt in a terminal or shell window is called "invoking the program from the command line."
- When you use this method, you can also type in additional information, called command line arguments, that the program can use.

  >     prog -v input.dat

- Only text mode programs receive command line arguments; the graphics library that comes with this book does not collect them.
- To receive command line arguments, you need to define the main function in a different way.

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

  - `argc` is the count of arguments.
  - `argv` contains the values of the arguments.
- Note that `argc` is an array of pointers to C character arrays.
- In the invocation above, `argc` is 3 and `argv` contains (pointers to) the strings

  > `argv[0]:` |  | `"prog"`
  > `argv[1]:` |  | `"-v"`
  > `argv[2]:` |  | `"input.dat"`

- Note that `argv[0]` always points to the name of the program and that `argc` is always at least 1.

previous | start | next