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

IT training learn c on the mac mark 2009 04 28 1

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 (5.58 MB, 378 trang )

CYAN
MAGENTA

YELLOW
BLACK
PANTONE 123 C

SPOT MATTE

Companion

eBook

Available

Learn C on the Mac

• Learn to program in the C language entirely on your Mac.
• Perfect for beginners—no previous programming experience
is required!

• This new version of the classic C primer from SpiderWorks

has been updated to bring you the latest C standards and best
practices.

T

his book is a primer to programming in general and to the C programming language specifically. You will not only learn how to program in C in
any environment but also take your first steps toward learning how to program
for the Mac as well as the iPhone. Learn C on the Mac offers a full course in


programming, covering such concepts as functions, variables, operators, data
types, file management, typecasting, unions, recursion, linked lists, binary
trees, and more.
I wrote this book because I love teaching people how to program and because I
wish this book had existed when I was first learning C. Learn C on the Mac is a
perfect starting point for any Mac owner who wants to learn programming
with the full power of C.
Dave Mark is a longtime Mac developer and author of more than a
dozen titles including Beginning iPhone Development, The Macintosh
Programming Primer series, and Ultimate Mac Programming.

RELATED TITLES

Learn C
on the Mac
SEE LAST PAGE FOR DETAILS ON $10 eBOOK VERSION

ISBN 978-1-4302-1809-8
53999

Mark

COMPANION eBOOK

A Complete Course in
C Programming for the Beginner

Dave Mark

US $39.99

Shelve in
Macintosh Programming

SOURCE CODE ONLINE

www.apress.com

User level:
Beginner–Intermediate

9 781430 218098

this print for content only—size & color not accurate

spine = 0.875" 376 page count

e
ed th g
at of in
pd n mm ac!
-U itio ra M
lly d og e
Fu th E Pr r th
ur c C r fo
Fo ssi me
a
Cl Pri

BOOKS FOR PROFESSIONALS BY PROFESSIONALS®




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 HAPT E R 4

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

C HAPT E R 5

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

C HAPT E R 6

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

C HAPT E R 7

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

C HAPT E R 8

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

C HAPT E R 9


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

C HAPT E R 10

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

C HAPT E R 11

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

C HAPT E R 12

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

APPEND IX

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

I NDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

v



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

C HAPT E R 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 HAPT E R 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 HAPT E R 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 11

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 H APT 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

APPENDIX

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


×