Command-Line Arguments

Before the modern graphical interface, there was the command-line interface. DOS and UNIX are examples. The command line is the line you type to run your program in a command-line environment. Suppose you have a program in a file named fuss. Then the command line to run it might look like this in UNIX:

$ fuss

Or it might look like this in DOS:

C> fuss

Command-line arguments are additional items on the same line.

% fuss -r Ginger

A C program can read those additional items for its own use (see Figure 11.7).

Figure 11.7. Command-line arguments.

A C program reads these items by using arguments to main(). Listing 11.27 ...

Get C Primer Plus, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.