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

IT training learn c on the mac mark 2009 04 28

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 (6.69 MB, 363 trang )


Learn C
on the Mac

DAVE MARK


Learn C on the Mac
Copyright © 2009 by Dave Mark
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic
or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the
prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-1809-8
ISBN-13 (electronic): 978-1-4302-1810-4
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a
trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with
no intention of infringement of the trademark.
Lead Editor: Clay Andres
Technical Reviewer: Kevin O’Malley
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell,
Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke,
Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Beth Christmas
Copy Editor: Heather Lang
Associate Production Director: Kari Brooks-Copony
Production Editor: Laura Esterman
Compositor/Artist/Interior Designer: Diana Van Winkle
Proofreader: Liz Welch
Indexer: Toma Mulligan
Cover Designer: Kurt Krames


Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or visit
.
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA
94705. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit .
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk
Sales–eBook Licensing web page at />The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has
been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or
entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information
contained in this work.
The source code for this book is available to readers at .


To my incredible family, D3KR4FR


Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

C H APT ER 1

Welcome Aboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

C H APT ER 2


Go Get the Tools!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

C H APT ER 3

Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17

C H APT ER 4

C Basics: Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25

C H APT ER 5

C Basics: Variables and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49

C H APT ER 6

Controlling Your Program’s Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85

C H APT ER 7

Pointers and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

C H APT ER 8

Variable Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

C H APT ER 9

Designing Your Own Data Structures . . . . . . . . . . . . . . . . . . . . . . . . 207


C H APT ER 1 0

Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

C H APT ER 1 1

Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

C H APT ER 1 2

Where Do You Go from Here?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

APPENDI X

Answers to Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

IN DEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

v


Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
C H APT ER 1

Welcome Aboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
Who Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

The Lay of the Land . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

C H APT ER 2

Go Get the Tools! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
Create an ADC Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Download the Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Installing the Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Take Your Tools for a Test Drive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
The Xcode Welcome Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
Creating Your First Xcode Project . . . . . . . . . . . . . . . . . . . . . . . . .11
Saving Your New Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
Running the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Downloading the Book Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
Let’s Move On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16

C H APT ER 3

Programming Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Some Alternatives to C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
What About Objective-C, C++, and Java?. . . . . . . . . . . . . . . . . .18
What’s the Best Language for Programming
the Mac or iPhone? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
The Programming Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
Compiling Your Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24


vii


viii

CONTENTS

C H APT ER 4

C Basics: Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
C Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25
The Function Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
Syntax Errors and Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27
Calling a Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
A Brief History of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32
The Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
Exploring Unix and Your Mac’s Built-In Manual . . . . . . . . . . . . . . . . .34
Same Program, Two Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
The hello2 Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
The hello2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40
Running hello2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42
Let’s Do That Again, Again, Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
Generating Some Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
C Is Case Sensitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47

C H APT ER 5

C Basics: Variables and Operators. . . . . . . . . . . . . . . 49
An Introduction to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50

Working with Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51
Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51
The Size of a Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52
Bytes and Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53
Going from 1 Byte to 2 Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56
The +, -, ++, and -- Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
The += and -= Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
The *, /, *=, and /= Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59
Using Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61
Sample Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63
Opening operator.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63
Stepping Through the operator Source Code. . . . . . . . . . . . . .64
Opening postfix.xcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67
Stepping Through the postfix Source Code . . . . . . . . . . . . . . .68
Backslash Combinations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
Support for Backslash Combinations . . . . . . . . . . . . . . . . . . . . . .71
Running slasher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
Stepping Through the slasher Source Code . . . . . . . . . . . . . . .73


CONTENTS

Building slasher the Unix Way . . . . . . . . . . . . . . . . . . . . . . . . . . . .74
Which Compiler Did You Run? . . . . . . . . . . . . . . . . . . . . . . . . . . . .76
Running the Unix Version of slasher. . . . . . . . . . . . . . . . . . . . . . .77
Sprucing Up Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77
Source Code Spacing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78
Comment Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80

The Curly Brace Controversy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
C H APT ER 6

Controlling Your Program’s Flow. . . . . . . . . . . . . . . . 85
Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85
The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
True Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Comparative Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90
truthTester.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94
Compound Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94
Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95
The Curly Braces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96
Where to Place the Semicolon . . . . . . . . . . . . . . . . . . . . . . . . . . . .98
The Loneliest Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98
The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98
The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
loopTester.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
The do Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Breaks in Other Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
isOdd.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Stepping Through the isOdd Source Code . . . . . . . . . . . . . . 112
nextPrime.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Stepping Through the nextPrime Source Code . . . . . . . . . . 115
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

C H APT ER 7


Pointers and Parameters . . . . . . . . . . . . . . . . . . . . . . 121
What Is a Pointer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Why Use Pointers? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Checking Out of the Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

ix


x

CONTENTS

Pointer Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Variable Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The & Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Declaring a Pointer Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Function Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Are Function Parameters? . . . . . . . . . . . . . . . . . . . . . . . .
Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How Function Parameters Work . . . . . . . . . . . . . . . . . . . . . . . .
Parameters Are Temporary . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Difference Between Arguments and Parameters . . . .
What Does All This Have to Do with Pointers? . . . . . . . . . . . . . . . .
Global Variables and Function Returns . . . . . . . . . . . . . . . . . . . . . . .
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Adding Globals to Your Programs . . . . . . . . . . . . . . . . . . . . . . .
Function Returns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
printf() Returns a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Danger! Avoid Uninitialized Return Values! . . . . . . . . . . . . . .

To Return or Not to Return?. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
More Sample Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
listPrimes.xcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
power.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C H APT ER 8

124
125
126
126
131
132
132
133
135
136
137
140
140
141
142
145
145
146
146
147
149
153

157

Variable Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Data Types Beyond int . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
floatSizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Walking Through the floatSizer Source Code . . . . . . . . . . . .
The Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Type Value Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Memory Efficiency vs. Safety. . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The ASCII Character Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ascii.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stepping Through the acsii Source Code . . . . . . . . . . . . . . . .
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Why Use Arrays? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
dice.xcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stepping Through the dice Source Code . . . . . . . . . . . . . . . .
Danger, Will Robinson! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

161
162
163
168
169
170
172
172
173
177
178

179
180
181
184


CONTENTS

Text Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Text String in Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
nameBad.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stepping Through the nameBad Source Code . . . . . . . . . . .
The Input Buffer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
On with the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Problem with nameBad . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The nameGood Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . .
The #define Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Function-Like #define Macros . . . . . . . . . . . . . . . . . . . . . . . . . .
wordCount.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stepping Through the wordCount Source Code . . . . . . . . .
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C H APT ER 9

185
185
186
187
188
190
191

193
194
196
198
199
203

Designing Your Own Data Structures . . . . . . . . . . 207
Bundling Your Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Model A: Three Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
multiArray.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stepping Through the multiArray Source Code . . . . . . . . . .
Getting Rid of the Extra Carriage Return . . . . . . . . . . . . . . . .
Adding Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Walking Through the multiArrayWithErrCode
Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Finishing Up With Model A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Model B: The Data Structure Approach . . . . . . . . . . . . . . . . . . . . . .
structSize.xcode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stepping Through the structSize Source Code. . . . . . . . . . .
Passing a struct As a Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing a Copy of the struct . . . . . . . . . . . . . . . . . . . . . . . . . . . .
paramAddress.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
struct Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Allocating Your Own Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using malloc() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
free() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Keeping Track of That Address! . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Why Use Linked Lists? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Creating a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
dvdTracker.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stepping Through the dvdTracker Source Code . . . . . . . . .
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

207
208
210
211
217
217
218
220
222
223
223
226
228
229
230
231
232
234
234
235
236
236
237
239
247


xi


xii

CONTENTS

C H APT ER 1 0

Working with Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
What Is a File?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Files: File Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding File Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Opening and Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Reading a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
printFile.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stepping Through the printFile Source Code . . . . . . . . . . . .
stdin, stdout, and stderr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Files: Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Writing to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
dvdFiler.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating a New Source Code File . . . . . . . . . . . . . . . . . . . . . . .
Exploring dvdData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Running dvdFiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stepping Through the dvdFiler Source Code . . . . . . . . . . . .
Working with Files: Fancier File Manipulation . . . . . . . . . . . . . . . .
The Update Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Random File Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Random Access Functions. . . . . . . . . . . . . . . . . . . . . . . .

dinoEdit.xcodeproj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stepping Through the dinoEdit Source Code . . . . . . . . . . . .
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

C H APT ER 1 1

250
250
250
251
253
255
256
258
259
259
259
260
261
262
264
272
272
273
274
274
275
281

Advanced Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

Typecasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cast with Care . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Casting with Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Why Use Unions?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Function Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Recursive Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Binary Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Searching Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recursion and Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
An Initializion Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Remaining Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

283
284
285
287
288
290
291
294
297
298
301
302
304
305



CONTENTS

Creating Your Own Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
More on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
strncpy() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
strncat() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
strncmp() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
strlen() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
More Standard Library Information . . . . . . . . . . . . . . . . . . . . .
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C HAPT ER 1 2

Where Do You Go from Here?. . . . . . . . . . . . . . . . . . 317
The Mac User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objective-C and Cocoa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Learning Cocoa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
One Last Bit of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Go Get ’Em . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

APPENDI X

308
309
310
312
312
313

313
313
314
314

318
318
319
320
322

Answers to Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . 323
Chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Chapter 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Chapter 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Chapter 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Chapter 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Chapter 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Chapter 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Chapter 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

324
327
328
329
330
332
333
334


I NDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

xiii


About the Author
Dave Mark has been writing about the Mac for the past 20 years. His
books include the Macintosh Programming Primer series (Addison-Wesley
1992), Beginning iPhone Development (Apress 2009), and the bestselling
previous editions of this book. Dave loves the water and spends as much
time as possible on it, in it, or near it. He lives with his wife and three
children in Virginia.

xv


About the
Technical Reviewer
Kevin O’Malley is a software engineer and author whose articles have appeared on the
Apple Developer Connection, O’Reilly MacDevCenter, and Dr. Dobb’s Journal web sites, as
well as in The Perl Journal and IEEE Internet Computing. He is the author of Programming Mac
OS X: A Guide for UNIX Developers (Manning Publications 2003). He worked at the University
of Michigan’s Artificial Intelligence laboratory for ten years as a software engineer. Kevin was
also an adjunct lecturer in the University of Michigan’s Department of Electrical Engineering
and Computer Science, where he taught two programming courses. He currently works at
Google in Mountain View, California.

xvii



Acknowledgments
This book could not have been written without the support of my wonderful family. Deneen,
Daniel, Kelley, and Ryan, thank you all for everything you’ve done for me. I truly am a lucky man.
Many, many thanks to the fine folks at Apress. My friend Clay Andres started this ball rolling
by bringing me over to Apress. Dominic Shakeshaft listened to my complaints with a gracious smile and great British charm. Beth Christmas, my esteemed project manager, pushed
when I needed pushing and kept me going when I was flagging. My production editor, Laura
Esterman, magically managed me across multiple books and kept me from getting things
hopelessly tangled. To Heather Lang, copy editor extraordinaire, I am very lucky to have you
as an editor. To Grace Wong and the production team, thank you with all my heart; it’s a great
pleasure to work with you. Thanks to Kari-Brooks Copony, who pulled together this gorgeous interior design, and to Diana Van Winkle for all the hard work of pouring a confused
set of Word files into this very finished product. Pete Aylward assembled the marketing message and got it out to the world. To all the folks at Apress, thank you, thank you, thank you!
A very special shout out goes to Kevin O’Malley, my incredibly talented tech reviewer. Kevin
made many important technical contributions to this book, helping me scrub the prose and
the sample code to ensure that it followed the C standard to the letter. Any divergence from
the standard is on me. Kevin, I owe you big time, buddy!
Finally, thanks to my friends and colleagues Jeff LaMarche, Dave Wooldridge, Todd Hitt, and
David Sobsey for your patience as I’ve taken time away from our projects to finish this book.
I’ll say it again—I am indeed a very lucky man.

xix


Preface
One of the best decisions I ever made was back in 1979 when I hooked up with my buddy
Tom Swartz and learned C. At first, C was just a meaningless scribble of curly brackets, semicolons, and parentheses. Fortunately for me, Tom was a C guru, and with him looking over
my shoulder, I learned C quickly.
Now it’s your turn.
This time, I’ll be looking over your shoulder as you learn C. My goal is to present every aspect
of C the way I would have liked it explained to me. I’ve saved up all the questions I had as I
learned the language and tried to answer them here.

Learning to program in C will open a wide range of opportunities for you. C is a tremendously
popular programming language and is the basis for Java, C++, and Objective-C. Whether you
want to start your own software company or just write programs for your own enjoyment, you
will discover that C programming is its own reward. Most of all, C programming is fun.
I hope you enjoy this book. If you have any suggestions or corrections, I’d love to hear from
you. In the meantime, turn the page, and let’s get started!

xxi


1

Chapter

Welcome Aboard

w

elcome! Chances are, you are reading this because you love the Mac. And not
only do you love the Mac, but you also love the idea of learning how to design
and develop your very own Mac programs.
You’ve definitely come to the right place.
This book assumes that you know how to use your Mac. That’s it. You don’t
need to know anything about programming, not one little bit. We’ll start off
with the basics, and each step we take will be a small one to make sure that
you have no problem following along.
This book will focus on the basics of programming. At the same time, you’ll
learn C, one of the most widely used programming languages in the world. And
once you know C, you’ll have a leg up on learning programming languages
like Objective-C, C++, and Java—all of which are based on C. If you are going

to write code these days, odds are good you’ll be writing it in one of these
languages.
Once you get through Learn C on the Mac, you’ll be ready to move on to
object-oriented programming and Objective-C, the official programming
language of Mac OS X. Not to worry; in this book, we’ll take small steps, so
nobody gets lost. You can definitely do this!

Who Is This Book For?
When I wrote the very first edition of Learn C on the Macintosh back in 1991,
I was writing with college students in mind. After all, in college was where I
really learned to program. It seems I was way off. My first clue that I had
underestimated my audience was when I started getting e-mails from fifth
graders who were making their way through the book. Fifth graders! And

1


2

CHAPTER 1: Welcome Aboard

not just one but lots of nine-, ten-, and eleven-year-old kids were digging in and learning
to program. Cool! And the best part of all was when these kids started sending me actual
shipping products that they created. You can’t imagine how proud I was and still am.
Over the years, I’ve heard from soccer moms, hobbyists, even folks who were using the Mac
for the very first time, all of whom made their way through Learn C on the Macintosh and
came out the other end, proud, strong, and full of knowledge.
So what do you need to know to get started? Although learning C by just reading a book
is possible, you’ll get the most out of this book if you run each example program as you
encounter it. To do this, you’ll need a Mac running Mac OS X (preferably version 10.5 or later)

and an Internet connection. You’ll need the Internet connection to download the free tools
Apple has graciously provided for anyone interested in programming the Mac and to download the programs that go along with this book.
Again, if you know nothing about programming, don’t worry. The first few chapters of this
book will bring you up to speed. If you have some programming experience (or even a lot),
you might want to skim the first few chapters, and then dig right into the C fundamentals
that start in Chapter 3.

The Lay of the Land
Here’s a quick tour of what’s to come in this book:


Q Chapter

2 takes you to Apple’s web site, so you can download the free tools we’ll use
throughout this book.



Q Chapter



Q Chapter



Q Chapter




Q Chapter



Q Chapter



Q Chapter

3 starts you off with the basics of building a simple program.

4 shows you how to embed a series of programming statements into a
reusable function, something you can call again and again.
5 adds variables and operators into the mix, bringing the power of mathematical expressions into your programs.
6 introduces the concept of flow control, using constructs like if, else, do,
and while to control the direction your program takes.
7 covers pointers and parameters, two concepts that will add a dramatic
new level of power to your programs.
8 extends the simple data types used in the first half of the book, adding the
ability to work with more complex data types like arrays and text strings.


CHAPTER 1: Welcome Aboard



Q Chapter

9 takes this concept one step further, adding the ability to design your own

custom data structures.



Q Chapter



Q Chapter



Q Finally,

10 shows you how to save your program’s data and read it back in again by
introducing the concept of the data file.
11 covers a variety of advanced topics—typecasting, unions, recursion,
binary trees, and much more.
Chapter 12 wraps things up and points you to the next step on your journey.

Ready to get started? Let’s go!

3


Chapter

2

Go Get the Tools!


b

efore we dig into the specifics of programming, you’ll need to download
a special set of tools from Apple’s web site. The good news is that these tools
are absolutely free. And, more importantly, Apple’s tools give you everything
you’ll need to create world-class Mac programs, whether they be written in C,
Objective-C, Java, or even C++.
To gain access to these tools, go to Apple’s web site and sign up as a member
of the Apple Developer Connection (ADC) program. The ADC web site offers
an incredible wealth of information designed to help programmers build
applications for the Mac and for the iPod Touch and iPhone.
Here’s the web address to the front page of the ADC site:


You’ll want to bookmark this page in your browser so you can refer to it later.
In fact, you might want to create a Learn C on the Mac bookmark folder in your
browser just for web sites I mention in this book.
Don’t let the sheer volume of information on this site overwhelm you. Over
time, that information will start to make a lot more sense. For now, let’s get in,
get the tools, and get out—no need to linger just yet.

Create an ADC Account
Before Apple will let you download the tools, you’ll first need to join ADC’s
iPhone Developer Program or Mac Developer Program. Both are free to join.
Since not everyone has an iPhone just yet, let’s stick with the Mac Developer
Program.

5



6

CHAPTER 2: Go Get the Tools!

On the front page of the ADC web site, click the button that says Visit Mac Dev Center or type
this URL into your browser:
/>
The Mac Dev Center is the part of ADC dedicated to all things related to Mac programming.
Look for the link that says sign-up. You’ll find it in the very first paragraph of text on the page,
as shown in Figure 2-1. The link will take you to a sign-up page, where you’ll fill out a form
and agree to the ADC terms and conditions.

Figure 2-1. Click the sign-up link on the Mac Dev Center front page.

Once you complete the sign-up process, make a note of your Apple ID and password. These
will come in incredibly useful. You’ll use them to log in each time you come back to the Mac
Dev Center. You’ll also be able to use your Apple ID in other areas of the Apple web site,
including Apple’s online store.

Download the Tools
Once you have your Apple ID, go back to the Mac Dev Center front page (http://
developer.apple.com/mac/).
If you are not already logged in, click the Log in button shown in Figure 2-1. Enter your
Apple ID and password, and you’ll be logged in and brought back to the Mac Dev Center
front page. But this time, you’ll have access to a variety of Mac Dev Center resources. Most
importantly, you’ll now be able to download Xcode, Apple’s suite of programming tools.
Take a look at Figure 2-2: notice the cursor pointing to the Xcode link. At the time of this
writing, version 3.1 was the latest and greatest version of Xcode. Don’t worry if the version
number is greater than 3.1. The project files that go along with this book will be updated to

run with the latest version of Xcode.


CHAPTER 2: Go Get the Tools!

Figure 2-2. Once you’re logged in to the Mac Dev Center site, find the link to the Xcode download page.

Before you click the Xcode download link, make sure you have plenty of space available on
your hard drive. The download file takes up more than a gigabyte (GB) of hard drive space.
And you’ll need space to unpack the file and still more space for your programming projects.
I would make sure you have at least 3GB of extra space on your drive.

CAUTION
It is always a good policy to leave 10 percent of your hard drive empty. That gives Mac OS X room to do its
behind-the-scenes maneuvering, with plenty of room to spare. If that 3GB you are saving for Xcode will
eat into your 10 percent empty space, consider moving some files off to another drive.

Once you’ve ensured that you have enough hard drive space, click the Xcode link. A few
seconds later, your browser will start downloading the Xcode .dmg file. A .dmg file is a disk
image, sort of like a virtual hard drive. You’ll see how to open your .dmg file in a moment.

7


8

CHAPTER 2: Go Get the Tools!

It’s a good idea to check the status of your download, to make sure the download has
actually started and to get a sense of how long it will take. Your web browser should have

a download window. In Safari, you open the download window by selecting Downloads
from the Window menu or by pressing ζδL. My download-in-progress is shown in
Figure 2-3. Note the magnifying glass icon on the right side of the Downloads window.
Click this icon once your download is complete to open a Finder window containing your
.dmg file. Click the icon, and let’s install the tools.

Figure 2-3. Safari’s download window showing the
Xcode download. Note the magnifying glass icon on
the right; it will reveal the .dmg file in the Finder.

Installing the Tools
Once your .dmg file download is complete, double-click the file in the Finder. The Finder
will open the .dmg file (see Figure 2-4) and mount the file on your desktop, just as if it were
a hard drive.

Figure 2-4. The Finder is opening and mounting our .dmg file.


CHAPTER 2: Go Get the Tools!

As you can see in Figure 2-5, the .dmg file will be mounted as a volume named Xcode Tools.
Click Xcode Tools, and double-click the icon labeled XcodeTools.mpkg. XcodeTools.mpkg is the
actual installer.

Figure 2-5. The .dmg file mounted as the volume named Xcode Tools

Once the installer launches, follow the instructions. In general, when given a choice, use the
default settings. In other words, don’t make any changes; just click the continue button. You
won’t be able to install Xcode on any drive other than your boot drive. Keep going until the
installer tells you the tools are installed. This will take a few minutes.


NOTE
Once the installer is finished installing the tools, eject the Xcode Tools volume, just as you would any other
volume. One way is to click the eject button to the right of the Xcode Tools entry in the Finder. Once the
volume is ejected, it will disappear from the Finder. Once you are certain that the tools were installed
properly, you might also want to delete the .dmg file from your downloads folder, just to save some hard
drive space.

9


10

CHAPTER 2: Go Get the Tools!

Take Your Tools for a Test Drive
Now that you’ve installed the tools, let’s explore. The first thing to note is the new Developer
folder at the top level of your hard drive. Go ahead and take a look. It is at the same level
as your Applications folder. As you make your way down your programming path, you will
spend a lot of time in the Developer folder.

TIP
Unix folks have created a very efficient system for describing where files live. Files and folders at the top
level of your hard drive start with a slash character “/”, which is followed by the file or folder name. Thus,
we might refer to /Applications or /Developer. To dive deeper, add another slash and another file or folder
name. For example, inside the Applications folder is a Utilities subfolder, and inside that is an application
named Terminal. Unix folks would refer to the Terminal application using this path: /Applications/Utilities/
Terminal.
Get the idea?


The tools package you just installed came with its own set of applications. They live inside
their own Applications folder within the Developer folder. Unix folks refer to this folder as
/Developer/Applications. We’ll use this Unix path naming convention throughout this book,
because it works really well.

TIP
To get to the /Developer folder using the Finder, click the icon for your main drive. Unless you renamed it,
it will have the name Macintosh HD.

In the Finder, navigate into /Developer/Applications. Inside that folder, you’ll find several
subfolders along with a number of applications. The most important of these applications is
the one named Xcode.
As you’ll learn throughout this book, Xcode is a program that helps you organize and build your
own programs. Technically, Xcode is known as an integrated development environment (IDE).
An IDE is a comprehensive, all-in-one, Swiss Army knife for programmers. IDEs typically feature
a sophisticated source code editor (for writing your programs) and a variety of tools for building,
running, testing, and analyzing your programs. As IDEs go, Xcode is quite a good one.
If you are new to programming, don’t let this detail overwhelm you. As you make your way
through this book, you’ll learn how to use Xcode to create, build, and run a ton of programs.


×