Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Section B.1. 

B.1.

B.1.1.

B.1.1.1. Example program

Example B-1 demonstrates the use of getopt() to parse the command line for two options: the -x option, which doesn’t expect an argument, and the -p option which does expect an argument. This program suppresses error messages from getopt() by specifying a colon (:) as the first character in optstring.

To allow us to observe the operation of getopt(), we include some printf() calls to display the information returned by each getopt() call. On completion, the program prints some summary information about the specified options and also displays the next nonoption word on the command line, if there is one. The following shell session log shows the results when we run this program with different command-line arguments:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial