Introduction ..........1
Who’s This Book For? ............. 2
What Makes This Book Different? .................. 2
This Book’s Design Elements ............ 3
How Can I Have Fun with C? ............... 4
What Do I Do Now? .................... 4
Part I: Jumping Right In
1 What Is C Programming, and Why Should I Care? ......5
What Is a Program? ........... 6
What You Need to Write C Programs ......... 7
The Programming Process .....10
Using C .....................11
2 Writing Your First C Program ..............13
A Down-and-Dirty Chunk of Code ...........14
The main() Function ..........16
Kinds of Data ..............17
Characters and C ...................18
Numbers in C ................19
Wrapping Things Up with Another Example Program ...............21
3 What Does This Do? Clarifying Your Code with Comments ...........23
Commenting on Your Code .......24
Specifying Comments .............25
Whitespace ...............27
A Second Style for Your Comments ...........28
4 Your World Premiere–Putting Your Program’s Results
Up on the Screen ........................31
How to Use printf() ..........32
The Format of printf() ....................32
Printing Strings .......................33
Escape Sequences ..................34
Conversion Characters .................36
Putting It All Together with a Code Example ...................38
5 Adding Variables to Your Programs .....41
Kinds of Variables ...........42
Naming Variables ..........43
Defining Variables .....................44
Storing Data in Variables ........................45
6 Adding Words to Your Programs ..................49
Understanding the String Terminator .......50
The Length of Strings ......................51
Character Arrays: Lists of Characters ..............52
Initializing Strings ...... 54
7 Making Your Programs More Powerful with #include and #define .......57
Including Files .....................58
Placing #include Directives ..............60
Defining Constants ...............60
Building a Header File and Program .............62
8 Interacting with Users ...............65
Looking at scanf() ...................66
Prompting for scanf()..............66
Problems with scanf() ............68
Part II: Putting C to Work for You with Operators and Expressions
9 Crunching the Numbers–Letting C Handle Math for You ...........73
Basic Arithmetic .............74
Order of Operators ..................77
Break the Rules with Parentheses ...........79
Assignments Everywhere .........................80
10 Powering Up Your Variables with Assignments and Expressions ..........83
Compound Assignment ..............84
Watch That Order! .................88
Typecasting: Hollywood Could Take Lessons from C ..........88
11 The Fork in the Road–Testing Data to Pick a Path .............91
Testing Data .................92
Using if ...................93
Otherwise…: Using else ......................96
12 Juggling Several Choices with Logical Operators ............ 103
Getting Logical ...................104
Avoiding the Negative ................109
The Order of Logical Operators ..................111
13 A Bigger Bag of Tricks–Some More Operators for Your Programs ...... 115
Goodbye if…else; Hello, Conditional .........116
The Small-Change Operators: ++ and -- .... 119
Sizing Up the Situation .................121
Part III: Fleshing Out Your Programs
14 Code Repeat–Using Loops to Save Time and Effort... 123
while We Repeat ...............124
Using while .............125
Using do…while ....................127
15 Looking for Another Way to Create Loops ........ 131
for Repeat’s Sake! ...........132
Working with for ..............134
16 Breaking in and out of Looped Code .......... 141
Take a break .................142
Let’s continue Working ..............145
17 Making the case for the switch Statement ............ 149
Making the switch ........151
break and switch................153
Efficiency Considerations ...........154
18 Increasing Your Program’s Output (and Input) ...... 163
putchar() and getchar() .............164
The Newline Consideration ............167
A Little Faster: getch() ....................169
19 Getting More from Your Strings ....... 171
Character-Testing Functions ..............172
Is the Case Correct? .........172
Case-Changing Functions.............176
String Functions ............176
20 Advanced Math (for the Computer, Not You!) ................ 181
Practicing Your Math ..............182
Doing More Conversions ...............183
Getting into Trig and Other Really Hard Stuff .............184
Getting Random ................187
Part IV: Managing Data with Your C Programs
21 Dealing with Arrays .......... 193
Reviewing Arrays ............194
Putting Values in Arrays ..........197
22 Searching Arrays .................. 201
Filling Arrays ....................202
Finders, Keepers ............................202
23 Alphabetizing and Arranging Your Data ......... 209
Putting Your House in Order: Sorting ............210
Faster Searches ......................215
24 Solving the Mystery of Pointers ................ 221
Memory Addresses ..........222
Defining Pointer Variables ...........222
Using the Dereferencing * ..................225
25 Arrays and Pointers .................. 231
Array Names Are Pointers ..................232
Getting Down in the List ...............233
Characters and Pointers .............234
Be Careful with Lengths ...............234
Arrays of Pointers ...................236
26 Maximizing Your Computer’s Memory ................. 243
Thinking of the Heap ......................244
But Why Do I Need the Heap? ............245
How Do I Allocate the Heap? .......246
If There’s Not Enough Heap Memory ............249
Freeing Heap Memory ...................250
Multiple Allocations ...........................250
27 Setting Up Your Data with Structures ............ 257
Defining a Structure ............................258
Putting Data in Structure Variables .................262
Part V: Files and Functions
28 Saving Sequential Files to Your Computer ................ 267
Disk Files................268
Opening a File .............................268
Using Sequential Files .........................270
29 Saving Random Files to Your Computer............ 277
Opening Random Files ............278
Moving Around in a File ...................279
30 Organizing Your Programs with Functions .................. 285
Form Follows C Functions ...................286
Local or Global?.................................290
31 Passing Variables to Your Functions ................... 293
Passing Arguments ............................294
Methods of Passing Arguments .......294
Passing by Value ........................295
Passing by Address ......................297
32 Returning Data from Your Functions ............ 305
Returning Values .....................306
The return Data Type ....................309
One Last Step: Prototype ..............309
Wrapping Things Up ................312
Appendixes
A The ASCII Table ..................... 313
B The Draw Poker Program ...................... 319
9780789751980, TOC, 7/18/2013