Tải bản đầy đủ (.pdf) (983 trang)

Stephen prata c primer plus 2005

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.65 MB, 983 trang )

800 East 96th St., Indianapolis, Indiana, 46240 USA
Primer Plus
Fifth Edition
C
Stephen Prata
00 0672326965 FM 10/19/04 1:52 PM Page i
C Primer Plus
Copyright © 2005 by Sams Publishing
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system,
or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise,
without written permission from the publisher. No patent liability is assumed with respect
to the use of the information contained herein. Although every precaution has been taken
in the preparation of this book, the publisher and author assume no responsibility for errors
or omissions. Nor is any liability assumed for damages resulting from the use of the infor-
mation contained herein.
International Standard Book Number: 0-672-32696-5
Library of Congress Catalog Card Number: 2004095068
Printed in the United States of America
First Printing: November, 2004
07 06 05 04 4321
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have
been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this infor-
mation. Use of a term in this book should not be regarded as affecting the validity of any
trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible, but
no warranty or fitness is implied. The information provided is on an “as is” basis.
Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk
purchases or special sales. For more information, please contact


U.S. Corporate and Government Sales
1-800-382-3419

For sales outside of the U.S., please contact
International Sales

ASSOCIATE PUBLISHER
Michael Stephens
ACQUISITIONS EDITOR
Loretta Yates
MANAGING EDITOR
Charlotte Clapp
DEVELOPMENT EDITOR
Songlin Qiu
PROJECT EDITOR
George E. Nedeff
COPY EDITOR
Bart Reed
INDEXER
Chris Barrick
PROOFREADER
Paula Lowell
TECHNICAL EDITOR
Greg Perry
PUBLISHING
COORDINATOR
Cindy Teeters
MULTIMEDIA DEVELOPER
Dan Scherf
BOOK DESIGNER

Gary Adair
PAGE LAYOUT
Bronkella Publishing
00 0672326965 FM 10/19/04 1:52 PM Page ii
CONTENTS AT A GLANCE
PREFACE XX
CHAPTER 1 Getting Ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
CHAPTER 2 Introducing C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
CHAPTER 3 Data and C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49
CHAPTER 4 Character Strings and Formatted Input/Output . . . . . . . . . . . . . . .89
CHAPTER 5 Operators, Expressions, and Statements . . . . . . . . . . . . . . . . . . .129
CHAPTER 6 C Control Statements: Looping . . . . . . . . . . . . . . . . . . . . . . . . . .169
CHAPTER 7 C Control Statements: Branching and Jumps . . . . . . . . . . . . . . . .221
CHAPTER 8 Character Input/Output and Input Validation . . . . . . . . . . . . . . .271
CHAPTER 9 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303
CHAPTER 10 Arrays and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347
CHAPTER 11 Character Strings and String Functions . . . . . . . . . . . . . . . . . . . .399
CHAPTER 12 Storage Classes, Linkage, and Memory Management . . . . . . . . . .453
CHAPTER 13 File Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .499
CHAPTER 14 Structures and Other Data Forms . . . . . . . . . . . . . . . . . . . . . . . .535
CHAPTER 15 Bit Fiddling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .597
CHAPTER 16 The C Preprocessor and the C Library . . . . . . . . . . . . . . . . . . . . .629
CHAPTER 17 Advanced Data Representation . . . . . . . . . . . . . . . . . . . . . . . . . .681
APPENDIXES
APPENDIX A Answers to the Review Questions . . . . . . . . . . . . . . . . . . . . . . . .759
APPENDIX B Reference Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .799
INDEX 889
00 0672326965 FM 10/19/04 1:52 PM Page iii
TABLE OF CONTENTS
CHAPTER 1 Getting Ready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1

Whence C? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
Why C? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Design Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2
Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Power and Flexibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Programmer Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Shortcomings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4
Whither C? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4
What Computers Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
High-level Computer Languages and Compilers . . . . . . . . . . . . . . . . . . . . . . . .6
Using C: Seven Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7
Step 1: Define the Program Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
Step 2: Design the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
Step 3: Write the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
Step 4: Compile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Step 5: Run the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Step 6: Test and Debug the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Step 7: Maintain and Modify the Program . . . . . . . . . . . . . . . . . . . . . . . . .10
Commentary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
Programming Mechanics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
Object Code Files, Executable Files, and Libraries . . . . . . . . . . . . . . . . . . .11
Unix System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
Linux System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Integrated Development Environments (Windows) . . . . . . . . . . . . . . . . . .15
DOS Compilers for the IBM PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
C on the Macintosh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Language Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
The First ANSI/ISO C Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
The C99 Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18

How This Book Is Organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
Typeface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
Program Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
Special Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
00 0672326965 FM 10/19/04 1:52 PM Page iv
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22
Programming Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22
CHAPTER 2 Introducing C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
A Simple Example of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
The Example Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
Pass 1: Quick Synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
Pass 2: Program Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
The Structure of a Simple Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Tips on Making Your Programs Readable . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
Taking Another Step in Using C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Multiple Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Printing Multiple Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
While You’re at It—Multiple Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Introducing Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40
Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
Semantic Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
Program State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42
Keywords and Reserved Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45

Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
CHAPTER 3 Data and C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49
A Sample Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49
What’s New in This Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51
Data Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52
Data: Data-Type Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52
Integer Versus Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
The Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
The Floating-Point Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
Basic C Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
The int Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
Other Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Using Characters: Type char . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64
The _Bool Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
Portable Types: inttypes.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
Types float, double, and long double . . . . . . . . . . . . . . . . . . . . . . . . . . .72
00 0672326965 FM 10/19/04 1:52 PM Page v
vi C PRIMER PLUS
Complex and Imaginary Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76
Beyond the Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77
Type Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79
Using Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80
Arguments and Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
One More Example: Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83
What Happens When the Program Runs . . . . . . . . . . . . . . . . . . . . . . . . . .83
Flushing the Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .84
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88

CHAPTER 4 Character Strings and Formatted Input/Output . . . . . . . . . . . . . . . .89
Introductory Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89
Character Strings: An Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91
Type char Arrays and the Null Character . . . . . . . . . . . . . . . . . . . . . . . . . .91
Using Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92
The strlen() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93
Constants and the C Preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95
The const Modifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98
Manifest Constants on the Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98
Exploring and Exploiting printf() and scanf() . . . . . . . . . . . . . . . . . . . . .101
The printf() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101
Using printf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102
Conversion Specification Modifiers for printf() . . . . . . . . . . . . . . . . . . .104
What Does a Conversion Specification Convert? . . . . . . . . . . . . . . . . . . .110
Using scanf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116
The * Modifier with printf() and scanf() . . . . . . . . . . . . . . . . . . . . . .120
Usage Tips for printf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127
CHAPTER 5 Operators, Expressions, and Statements . . . . . . . . . . . . . . . . . . . . .129
Introducing Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
Fundamental Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132
Assignment Operator: = . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132
Addition Operator: + . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134
Subtraction Operator: – . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134
00 0672326965 FM 10/19/04 1:52 PM Page vi
viiCONTENTS
Sign Operators: – and + . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134

Multiplication Operator:
* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135
Division Operator: / . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
Precedence and the Order of Evaluation . . . . . . . . . . . . . . . . . . . . . . . . .140
Some Additional Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141
The sizeof Operator and the size_t Type . . . . . . . . . . . . . . . . . . . . . . .142
Modulus Operator: % . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
Increment and Decrement Operators: ++ and . . . . . . . . . . . . . . . . . . .144
Decrementing: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148
Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149
Don’t Be Too Clever . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149
Expressions and Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150
Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151
Compound Statements (Blocks) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154
Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156
The Cast Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158
Function with Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159
A Sample Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167
CHAPTER 6 C Control Statements: Looping . . . . . . . . . . . . . . . . . . . . . . . . . . .169
Revisiting the while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170
Program Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170
C-Style Reading Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172
The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172
Terminating a while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173

When a Loop Terminates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174
while: An Entry-Condition Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174
Syntax Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175
Which Is Bigger: Using Relational Operators and Expressions . . . . . . . . . . .176
What Is Truth? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178
What Else Is True? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179
Troubles with Truth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180
The New _Bool Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182
Precedence of Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183
00 0672326965 FM 10/19/04 1:52 PM Page vii
viii C PRIMER PLUS
Indefinite Loops and Counting Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186
The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187
Using for for Flexibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188
More Assignment Operators: +=, -=, *=, /=, %= . . . . . . . . . . . . . . . . . . . . . . .193
The Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .193
Zeno Meets the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196
An Exit-Condition Loop: do while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198
Which Loop? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201
Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201
Program Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202
A Nested Variation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202
Introducing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203
Using a for Loop with an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205
A Loop Example Using a Function Return Value . . . . . . . . . . . . . . . . . . . . .207
Program Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209
Using Functions with Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . .210
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212

Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217
CHAPTER 7 C Control Statements: Branching and Jumps . . . . . . . . . . . . . . . . .221
The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222
Adding else to the if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224
Another Example: Introducing getchar() and putchar() . . . . . . . . . . .225
The ctype.h Family of Character Functions . . . . . . . . . . . . . . . . . . . . . .228
Multiple Choice else if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230
Pairing else with if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233
More Nested ifs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234
Let’s Get Logical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238
Alternate Spellings: The iso646.h Header File . . . . . . . . . . . . . . . . . . . .239
Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240
Order of Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240
Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242
A Word-Count Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242
The Conditional Operator: ?: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246
Loop Aids: continue and break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248
The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248
The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251
Multiple Choice: switch and break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253
Using the switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255
Reading Only the First Character of a Line . . . . . . . . . . . . . . . . . . . . . . . .256
00 0672326965 FM 10/19/04 1:52 PM Page viii
ixCONTENTS
Multiple Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257
switch and if else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259
The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259
Avoiding goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263

Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267
CHAPTER 8 Character Input/Output and Input Validation . . . . . . . . . . . . . . . .271
Single-Character I/O: getchar() and putchar() . . . . . . . . . . . . . . . . . . . . .272
Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273
Terminating Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274
Files, Streams, and Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274
The End of File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275
Redirection and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .278
Unix, Linux, and DOS Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .279
Creating a Friendlier User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283
Working with Buffered Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283
Mixing Numeric and Character Input . . . . . . . . . . . . . . . . . . . . . . . . . . .285
Input Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .288
Analyzing the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .292
The Input Stream and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293
Menu Browsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293
Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .294
Toward a Smoother Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .294
Mixing Character and Numeric Input . . . . . . . . . . . . . . . . . . . . . . . . . . .296
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .300
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .301
CHAPTER 9 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303
Reviewing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303
Creating and Using a Simple Function . . . . . . . . . . . . . . . . . . . . . . . . . . .304
Analyzing the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305
Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308
Defining a Function with an Argument: Formal Parameters . . . . . . . . . . .309

Prototyping a Function with Arguments . . . . . . . . . . . . . . . . . . . . . . . . .310
Calling a Function with an Argument: Actual Arguments . . . . . . . . . . . . .311
The Black-Box Viewpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311
Returning a Value from a Function with return . . . . . . . . . . . . . . . . . . .312
Function Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315
00 0672326965 FM 10/19/04 1:52 PM Page ix
ANSI C Function Prototyping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316
The ANSI Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318
No Arguments and Unspecified Arguments . . . . . . . . . . . . . . . . . . . . . . .319
Hooray for Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320
Recursion Revealed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320
Recursion Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .322
Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323
Recursion and Reversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325
Recursion Pros and Cons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .327
Compiling Programs with Two or More Source Code Files . . . . . . . . . . . . . .328
Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .328
Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .328
DOS Command-Line Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329
Windows and Macintosh Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . .329
Using Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329
Finding Addresses: The & Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .333
Altering Variables in the Calling Function . . . . . . . . . . . . . . . . . . . . . . . . . . .334
Pointers: A First Look . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .336
The Indirection Operator: * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337
Declaring Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .338
Using Pointers to Communicate Between Functions . . . . . . . . . . . . . . . .339
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .345
CHAPTER 10 Arrays and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347
Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .348
Designated Initializers (C99) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .352
Assigning Array Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353
Array Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .354
Specifying an Array Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .356
Initializing a Two-Dimensional Array . . . . . . . . . . . . . . . . . . . . . . . . . . . .359
More Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .360
Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361
Functions, Arrays, and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364
Using Pointer Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366
Comment: Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .369
x C PRIMER PLUS
00 0672326965 FM 10/19/04 1:52 PM Page x
Pointer Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .369
Protecting Array Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .374
Using
const with Formal Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . .374
More About const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .376
Pointers and Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . .378
Pointers to Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . .381
Pointer Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .382
Functions and Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . .383
Variable-Length Arrays (VLAs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .386
Compound Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .390

Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .393
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .393
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .395
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397
CHAPTER 11 Character Strings and String Functions . . . . . . . . . . . . . . . . . . . .399
Representing Strings and String I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .399
Defining Strings Within a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .401
Pointers and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .407
String Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .409
Creating Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .409
The gets() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .410
The fgets() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .412
The scanf() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .413
String Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .414
The puts() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .415
The fputs() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .416
The printf() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417
The Do-It-Yourself Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417
String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .420
The strlen() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .420
The strcat() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .421
The strncat() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .422
The strcmp() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .423
The strncmp() Variation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .427
The strcpy() and strncpy() Functions . . . . . . . . . . . . . . . . . . . . . . . . .428
The sprintf() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433
Other String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .434
A String Example: Sorting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .436
Sorting Pointers Instead of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .437
The Selection Sort Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .438

xiCONTENTS
00 0672326965 FM 10/19/04 1:52 PM Page xi
xii C PRIMER PLUS
The ctype.h Character Functions and Strings . . . . . . . . . . . . . . . . . . . . . . .439
Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .441
Command-Line Arguments in Integrated Environments . . . . . . . . . . . . .443
Command-Line Arguments with the Macintosh . . . . . . . . . . . . . . . . . . . .443
String-to-Number Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .444
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .446
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .447
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .448
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .451
CHAPTER 12 Storage Classes, Linkage, and Memory Management . . . . . . . . . .453
Storage Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .453
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .454
Linkage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .455
Storage Duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .456
Automatic Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .457
Register Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .461
Static Variables with Block Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .461
Static Variables with External Linkage . . . . . . . . . . . . . . . . . . . . . . . . . . .463
Static Variables with Internal Linkage . . . . . . . . . . . . . . . . . . . . . . . . . . . .468
Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .468
Storage-Class Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469
Storage Classes and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471
Which Storage Class? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .472
A Random-Number Function and a Static Variable . . . . . . . . . . . . . . . . . . . .472
Roll ’Em . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .476
Allocated Memory: malloc() and free() . . . . . . . . . . . . . . . . . . . . . . . . . .480
The Importance of free() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .483

The calloc() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .484
Dynamic Memory Allocation and Variable-Length Arrays . . . . . . . . . . . .485
Storage Classes and Dynamic Memory Allocation . . . . . . . . . . . . . . . . . .486
ANSI C Type Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .486
The const Type Qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .486
The volatile Type Qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489
The restrict Type Qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .490
New Places for Old Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .491
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .492
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .492
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .495
00 0672326965 FM 10/19/04 1:52 PM Page xii
CHAPTER 13 File Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .499
Communicating with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .499
What Is a File? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .500
The Text View and the Binary View . . . . . . . . . . . . . . . . . . . . . . . . . . . . .500
Levels of I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .501
Standard Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .501
Standard I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .502
Checking for Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . .503
The
fopen() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .504
The getc() and putc() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .505
End-of-File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .505
The fclose() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .507
Pointers to the Standard Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .507
A Simple-Minded File-Condensing Program . . . . . . . . . . . . . . . . . . . . . . . . .507
File I/O: fprintf(), fscanf(), fgets(), and fputs() . . . . . . . . . . . . . . . .509
The fprintf() and fscanf() Functions . . . . . . . . . . . . . . . . . . . . . . . . .509

The fgets() and fputs() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .511
Commentary: gets() and fgets() . . . . . . . . . . . . . . . . . . . . . . . . . . . . .513
Adventures in Random Access: fseek() and ftell() . . . . . . . . . . . . . . . . .513
How fseek() and ftell() Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .514
Binary Versus Text Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .516
Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .516
The fgetpos() and fsetpos() Functions . . . . . . . . . . . . . . . . . . . . . . . .517
Behind the Scenes with Standard I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .517
Other Standard I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .518
The int ungetc(int c, FILE *fp) Function . . . . . . . . . . . . . . . . . . . .519
The int fflush() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .519
The int setvbuf() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .519
Binary I/O: fread() and fwrite() . . . . . . . . . . . . . . . . . . . . . . . . . . . . .520
The size_t fwrite() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .521
The size_t fread() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .522
The int feof(FILE *fp) and int ferror(FILE *fp) Functions . . . . .522
An fread() and fwrite() Example . . . . . . . . . . . . . . . . . . . . . . . . . . . .523
Random Access with Binary I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .525
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .527
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .528
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .529
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .530
CHAPTER 14 Structures and Other Data Forms . . . . . . . . . . . . . . . . . . . . . . . .535
Sample Problem: Creating an Inventory of Books . . . . . . . . . . . . . . . . . . . . .535
Setting Up the Structure Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .537
xiiiCONTENTS
00 0672326965 FM 10/19/04 1:52 PM Page xiii
Defining a Structure Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .538
Initializing a Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .539
Gaining Access to Structure Members . . . . . . . . . . . . . . . . . . . . . . . . . . .540

Designated Initializers for Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . .540
Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .541
Declaring an Array of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .543
Identifying Members of an Array of Structures . . . . . . . . . . . . . . . . . . . . .543
Program Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544
Nested Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .545
Pointers to Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .547
Declaring and Initializing a Structure Pointer . . . . . . . . . . . . . . . . . . . . . .548
Member Access by Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .549
Telling Functions About Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .550
Passing Structure Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .550
Using the Structure Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .551
Passing a Structure as an Argument . . . . . . . . . . . . . . . . . . . . . . . . . . . . .552
More on Structure Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .553
Structures or Pointer to Structures? . . . . . . . . . . . . . . . . . . . . . . . . . . . . .557
Character Arrays or Character Pointers in a Structure . . . . . . . . . . . . . . .558
Structure, Pointers, and malloc() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .559
Compound Literals and Structures (C99) . . . . . . . . . . . . . . . . . . . . . . . . .561
Flexible Array Members (C99) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .562
Functions Using an Array of Structures . . . . . . . . . . . . . . . . . . . . . . . . . .565
Saving the Structure Contents in a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . .566
A Structure-Saving Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .567
Program Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .570
Structures: What Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .571
Unions: A Quick Look . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .572
Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .575
enum Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .575
Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .576
Assigned Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .576
enum Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .576

Shared Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .578
typedef: A Quick Look . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .578
Fancy Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .580
Functions and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .582
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .589
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .589
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .590
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .593
xiv C PRIMER PLUS
00 0672326965 FM 10/19/04 1:52 PM Page xiv
CHAPTER 15 Bit Fiddling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .597
Binary Numbers, Bits, and Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .597
Binary Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .598
Signed Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .599
Binary Floating Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .599
Other Number Bases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .600
Octal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .600
Hexadecimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .601
C’s Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .602
Bitwise Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .602
Usage: Masks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .604
Usage: Turning Bits On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .605
Usage: Turning Bits Off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .605
Usage: Toggling Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .606
Usage: Checking the Value of a Bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .606
Bitwise Shift Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .606
Programming Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .608
Another Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .610
Bit Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .612
Bit-Field Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .614

Bit Fields and Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .617
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .624
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .624
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .625
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .627
CHAPTER 16 The C Preprocessor and the C Library . . . . . . . . . . . . . . . . . . . . .629
First Steps in Translating a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .630
Manifest Constants: #define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .630
Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .634
Redefining Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .635
Using Arguments with #define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .635
Creating Strings from Macro Arguments: The # Operator . . . . . . . . . . . . .638
Preprocessor Glue: The ## Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . .639
Variadic Macros: and _ _VA_ARGS_ _ . . . . . . . . . . . . . . . . . . . . . . . .640
Macro or Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .641
File Inclusion: #include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .642
Header Files: An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .643
Uses for Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .646
Other Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .647
The #undef Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .647
Being Defined—The C Preprocessor Perspective . . . . . . . . . . . . . . . . . . .647
xvCONTENTS
00 0672326965 FM 10/19/04 1:52 PM Page xv
Conditional Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .648
Predefined Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .653
#line and #error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .654
#pragma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .654
Inline Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .655
The C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .658
Gaining Access to the C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .658

Using the Library Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .659
The Math Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .660
The General Utilities Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .663
The exit() and atexit() Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .663
The qsort() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .665
The Assert Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .670
memcpy() and memmove() from the string.h Library . . . . . . . . . . . . . . . . . .672
Variable Arguments: stdarg.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .674
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .676
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .677
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .677
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .678
CHAPTER 17 Advanced Data Representation . . . . . . . . . . . . . . . . . . . . . . . . . .681
Exploring Data Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .682
Beyond the Array to the Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .684
Using a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .687
Afterthoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .691
Abstract Data Types (ADTs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .692
Getting Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .693
Building an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .694
Using the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .699
Implementing the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .701
Getting Queued with an ADT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .708
Defining the Queue Abstract Data Type . . . . . . . . . . . . . . . . . . . . . . . . . .708
Defining an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .709
Implementing the Interface Data Representation . . . . . . . . . . . . . . . . . . .710
Testing the Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .719
Simulating with a Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .721
The Linked List Versus the Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .726
Binary Search Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .730

A Binary Tree ADT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .731
The Binary Search Tree Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .732
The Binary Tree Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .734
xvi C PRIMER PLUS
00 0672326965 FM 10/19/04 1:52 PM Page xvi
Trying the Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .750
Tree Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .754
Other Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .756
Key Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .756
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .757
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .757
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .758
APPENDIX A Answers to the Review Quesions . . . . . . . . . . . . . . . . . . . . . . . . .759
Answers to Review Questions for Chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . .759
Answers to Review Questions for Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . .760
Answers to Review Questions for Chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . .761
Answers to Review Questions for Chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . .764
Answers to Review Questions for Chapter 5 . . . . . . . . . . . . . . . . . . . . . . . . .767
Answers to Review Questions for Chapter 6 . . . . . . . . . . . . . . . . . . . . . . . . .770
Answers to Review Questions for Chapter 7 . . . . . . . . . . . . . . . . . . . . . . . . .773
Answers to Review Questions for Chapter 8 . . . . . . . . . . . . . . . . . . . . . . . . .776
Answers to Review Questions for Chapter 9 . . . . . . . . . . . . . . . . . . . . . . . . .777
Answers to Review Questions for Chapter 10 . . . . . . . . . . . . . . . . . . . . . . . .779
Answers to Review Questions for Chapter 11 . . . . . . . . . . . . . . . . . . . . . . . .782
Answers to Review Questions for Chapter 12 . . . . . . . . . . . . . . . . . . . . . . . .785
Answers to Review Questions for Chapter 13 . . . . . . . . . . . . . . . . . . . . . . . .786
Answers to Review Questions for Chapter 14 . . . . . . . . . . . . . . . . . . . . . . . .789
Answers to Review Questions for Chapter 15 . . . . . . . . . . . . . . . . . . . . . . . .792
Answers to Review Questions for Chapter 16 . . . . . . . . . . . . . . . . . . . . . . . .793
Answers to Review Questions for Chapter 17 . . . . . . . . . . . . . . . . . . . . . . . .795

APPENDIX B Reference Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .799
Section I: Additional Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .799
Magazine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .799
Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .799
C Language Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .801
Programming Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .801
Reference Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .801
C++ Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .802
Section II: C Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .802
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .803
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .804
Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .804
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .805
The Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .805
Pointer-Related Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .806
Sign Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .806
xviiCONTENTS
00 0672326965 FM 10/19/04 1:52 PM Page xvii
Structure and Union Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .806
Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .807
Miscellaneous Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .808
Section III: Basic Types and Storage Classes . . . . . . . . . . . . . . . . . . . . . . . . .808
Summary: The Basic Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .808
Summary: How to Declare a Simple Variable . . . . . . . . . . . . . . . . . . . . . .811
Summary: Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .812
Section IV: Expressions, Statements, and Program Flow . . . . . . . . . . . . . . . .813
Summary: Expressions and Statements . . . . . . . . . . . . . . . . . . . . . . . . . .813
Summary: The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .814
Summary: The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .815
Summary: The do while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . .815

Summary: Using if Statements for Making Choices . . . . . . . . . . . . . . . .816
Summary: Multiple Choice with switch . . . . . . . . . . . . . . . . . . . . . . . . .817
Summary: Program Jumps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .818
Section V: The Standard ANSI C Library with C99 Additions . . . . . . . . . . . .819
Diagnostics: assert.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .820
Complex Numbers: complex.h (C99) . . . . . . . . . . . . . . . . . . . . . . . . . . .820
Character Handling: ctype.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .822
Error Reporting: errno.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .823
Floating-Point Environment: fenv.h (C99) . . . . . . . . . . . . . . . . . . . . . . .824
Format Conversion of Integer Types: inttypes.h (C99) . . . . . . . . . . . . .826
Localization: locale.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .827
Math Library: math.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .830
Non-Local Jumps: setjmp.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .835
Signal Handling: signal.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .836
Variable Arguments: stdarg.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .837
Boolean Support: stdbool.h (C99) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .838
Common Definitions: stddef.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .838
Integer Types: stdint.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .839
Standard I/O Library: stdio.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .843
General Utilities: stdlib.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .846
String Handling: string.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .853
Type-Generic Math: tgmath.h (C99) . . . . . . . . . . . . . . . . . . . . . . . . . . . .857
Date and Time: time.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .857
Extended Multibyte and Wide-Character Utilities: wchar.h (C99) . . . . . .861
Wide Character Classification and Mapping Utilities: wctype.h (C99) . . .868
Section VI: Extended Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .871
Exact-Width Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .872
Minimum-Width Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .872
Fastest Minimum-Width Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .873
xviii C PRIMER PLUS

00 0672326965 FM 10/19/04 1:52 PM Page xviii
Maximum-Width Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .874
Integers That Can Hold Pointer Values . . . . . . . . . . . . . . . . . . . . . . . . . . .874
Extended Integer Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .874
Section VII: Expanded Character Support . . . . . . . . . . . . . . . . . . . . . . . . . .875
Trigraph Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .875
Digraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .876
Alternative Spellings:
iso646.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .876
Multibyte Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .877
Universal Character Names (UCNs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .877
Wide Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .878
Wide Characters and Multibyte Characters . . . . . . . . . . . . . . . . . . . . . . .879
Section VIII: C99 Numeric Computational Enhancements . . . . . . . . . . . . . .879
The IEC Floating-Point Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .880
The fenv.h Header File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .880
The STDC FP_CONTRACT Pragma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .881
Additions to the math.h Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .881
Support for Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .882
Section IX: Differences Between C and C++ . . . . . . . . . . . . . . . . . . . . . . . . .883
Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .884
char Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .884
The const Modifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .885
Structures and Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .886
Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .886
Pointer-to-void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .887
Boolean Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .887
Alternative Spellings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .887
Wide-Character Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .887
Complex Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .888

Inline Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .888
C99 Features Not Found in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .888
xixCONTENTS
00 0672326965 FM 10/19/04 1:52 PM Page xix
PREFACE
C was a relatively little-known language when the first edition of C Primer Plus was written in
1984. Since then, the language has boomed, and many people have learned C with the help of
this book. In fact, over 500,000 people have purchased C Primer Plus throughout its various
editions.
As the language has grown from the early informal K&R standard through the 1990 ISO/ANSI
standard to the 1999 ISO/ANSI standard, so has this book matured through this, the fifth edi-
tion. As with all the editions, my aim has been to create an introduction to C that is instruc-
tive, clear, and helpful.
Approach and Goals
My goal is for this book to serve as a friendly, easy-to-use, self-study guide. To accomplish that
objective, C Primer Plus employs the following strategies:
•Programming concepts are explained, along with details of the C language; the book
does not assume that you are a professional programmer.
• Many short, easily typed examples illustrate just one or two concepts at a time, because
learning by doing is one of the most effective ways to master new information.
•Figures and illustrations clarify concepts that are difficult to grasp in words alone.
•Highlight boxes summarize the main features of C for easy reference and review.
• Review questions and programming exercises at the end of each chapter allow you to
test and improve your understanding of C.
To gain the greatest benefit, you should take as active a role as possible in studying the topics
in this book. Don’t just read the examples, enter them into your system, and try them. C is a
very portable language, but you may find differences between how a program works on your
system and how it works on ours. Experiment—change part of a program to see what the
effect is. Modify a program to do something slightly different. Ignore the occasional warnings
and see what happens when you do the wrong thing. Try the questions and exercises. The

more you do yourself, the more you will learn and remember.
I hope that you’ll find this newest edition an enjoyable and effective introduction to the C lan-
guage.
00 0672326965 FM 10/19/04 1:52 PM Page xx
ABOUT THE AUTHOR
Stephen Prata teaches astronomy, physics, and programming at the College of Marin in
Kentfield, California. He received his B.S. from the California Institute of Technology and his
Ph.D. from the University of California, Berkeley. His association with computers began with
the computer modeling of star clusters. Stephen has authored or coauthored over a dozen
books, including C++ Primer Plus and Unix Primer Plus.
00 0672326965 FM 10/19/04 1:52 PM Page xxi
DEDICATION
With love to Vicky and Bill Prata, who, for more than 69 years, have been showing
how rewarding a marriage can be. —SP
ACKNOWLEDGMENTS
I wish to thank Loretta Yates of Sams Publishing for getting this project underway and Songlin
Qiu of Sams Publishing for seeing it through. Also, thank you Ron Liechty of Metrowerks and
Greg Comeau of Comeau Computing for your help with new C99 features and your notewor-
thy commitment to customer service.
00 0672326965 FM 10/19/04 1:52 PM Page xxii
WE WANT TO HEAR FROM YOU!
As the reader of this book, you are our most important critic and commentator. We value your
opinion and want to know what we’re doing right, what we could do better, what areas you’d
like to see us publish in, and any other words of wisdom you’re willing to pass our way.
As an associate publisher for Sams Publishing, I welcome your comments. You can email or
write me directly to let me know what you did or didn’t like about this book—as well as what
we can do to make our books better.
Please note that I cannot help you with technical problems related to the topic of this book. We
do have a User Services group, however, where I will forward specific technical questions related
to the book.

When you write, please be sure to include this book’s title and author as well as your name,
email address, and phone number. I will carefully review your comments and share them with
the author and editors who worked on the book.
Email:
Mail: Michael Stephens
Associate Publisher
Sams Publishing
800 East 96th Street
Indianapolis, IN 46240 USA
For more information about this book or another Sams Publishing title, visit our Web site at
www.samspublishing.com. Type the ISBN (0672326965) or the title of a book in the Search
field to find the page you’re looking for.
00 0672326965 FM 10/19/04 1:52 PM Page xxiii
00 0672326965 FM 10/19/04 1:52 PM Page xxiv
CHAPTER 1
GETTING READY
You will learn about the following in this chapter:
•C’s history and features
• The steps needed to write
programs
•A bit about compilers and linkers
•C standards
elcome to the world of C—a vigorous, professional programming language popular
with amateur and commercial programmers alike. This chapter prepares you for
learning and using this powerful and popular language, and it introduces you to the
kinds of environments in which you will most likely develop your C-legs.
First, we look at C’s origin and examine some of its features, both strengths and drawbacks.
Then we look at the origins of programming and examine some general principles for pro-
gramming. Finally, we discuss how to run C programs on some common systems.
Whence C?

Dennis Ritchie of Bell Labs created C in 1972 as he and Ken Thompson worked on designing
the Unix operating system. C didn’t spring full-grown from Ritchie’s head, however. It came
from Thompson’s B language, which came from… but that’s another story. The important
point is that C was created as a tool for working programmers, so its chief goal is to be a useful
language.
Most languages aim to be useful, but they often have other concerns. The main goal for Pascal,
for instance, was to provide a sound basis for teaching good programming principles. BASIC,
on the other hand, was developed to resemble English so that it could be learned easily by stu-
dents unfamiliar with computers. These are important goals, but they are not always compati-
ble with pragmatic, workaday usefulness. C’s development as a language designed for
programmers, however, has made it one of the modern-day languages of choice.
W
01 0672326965 CH01 10/19/04 1:53 PM Page 1

×