Chapter 2. Arguments, Options, and the Environment

In this chapter

  • 2.1 Option and Argument Conventions page 24

  • 2.2 Basic Command-Line Processing page 28

  • 2.3 Option Parsing: getopt() and getopt_long() page 30

  • 2.4 The Environment page 40

  • 2.5 Summary page 49

  • Exercises page 50

Command-line option and argument interpretation is usually the first task of any program. This chapter examines how C (and C++) programs access their command-line arguments, describes standard routines for parsing options, and takes a look at the environment.

Option and Argument Conventions

The word arguments has two meanings. The more technical definition is “all the ’words’ on the command line.” For example:

$ ls main.c opts.c process.c

Here, the user typed four “words.” All four words ...

Get Linux® Programming by Example 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.