Sie sind auf Seite 1von 10

Practical C Programming

Third Edition

Steve Oualline

O'REILLY®
Beging • Cambridge • Farnham • Köln • Sebastopol • Tokyo
Table of Contents

Preface ................................................................................ xv
How This Book is Organized ...................................................................... xvi
Chapter by Chapter ...................................................................................... xvi
Notes on the Third Edition .......................................................................... xix
Font Conventions ......................................................................................... xix
Ohtaining Source Code ................................................................................. xx
Comments and Questions .......................................................................... xxii
Acknowledgments ....................................................................................... xxii
Acknowledgments to the Third Edition .................................................... xxiii

L Basics ................................................................................... 1
1. What Is C?............................................................................ 3
How Programming Works .............................................................................. 4
Brief History of C ............................................................................................ 6
How C Works .................................................................................................. 7
How to Learn C ............................................................................................... 9

2. Basics of Program Writing ................................................ 11


Programs from Conception to Execution ..................................................... 11
Creating a Real Program ............................................................................... 12
Creating a Program Using a Command-Line Compiler ................................ 13
Creating a Program Using an Integrated Development Environment .......... 16
Getting Help on UNIX .................................................................................. 33
vi Table of Contents

Getting HeIp in an Integrated Development Environment ............................ 34


IDE Cookhooks ....................................................................................................... 34
Programming Exercises .......................................................................................... 37

3. Style .................................................................................... 38
Common Coding Practices .................................................................................... 42
Coding Religion ....................................................................................................... 45
Indentation and Code Format .............................................................................. 45
Clarity ........................................................................................................................ 46
Simplicity .................................................................................................................. 47
Su mmary ................................................................................................................... 48

4. Basic Declarations and Expressions ................................. 49


Elements of a Program .......................................................................................... 49
Basic Program Structure ......................................................................................... 50
Simple Expressions ................................................................................................ 51
Variables and Storage ............................................................................................ 52
Variable Declarations ............................................................................................. 53
Integers ..................................................................................................................... 54
Assignment Statements .......................................................................................... 54
printf Function ......................................................................................................... 56
Floating Point .......................................................................................................... 57
Floating Point Versus Integer Divicle .................................................................. 58
Characters ................................................................................................................. 60
Answers .................................................................................................................... 61
Programming Exercises 62

5. Arrays, Qualifiers, and Reading Numbers ....................... 63


Arrays ........................................................................................................................ 63
Strings ....................................................................................................................... 64
Reading Strings ........................................................................................................ 67
Multidimensional Arrays ........................................................................................ 70
Reading Numbers .................................................................................................... 71
lnitializing Variables ................................................................................................ 73
Integers
Typesof .................................................................................................... 75
Floats
Typesof ........................................................................................................ 77
Constant Declarations .... .. 77
Table of Contents vif

Hexadecimal and Octal Constants ................................................................ 78


Operators for Performing Shortcuts .............................................................. 78
Side Effects .................................................................................................... 79
++x or x++ ..................................................................................................... 80
More Side-Effect Problems ........................................................................... 81
Answers ......................................................................................................... 82
Programming Exercises 83

6 Decision and Control Statements ....................................... 84


if Statement .................................................................................................... 84
else Statement ............................................................................................... 85
How Not to Use strcmp ................................................................................ 86
Looping Statements ....................................................................................... 87
while Statement ............................................................................................. 87
break Statement 89
continuc Statement .......................................................................................
Assignment Anywherc Side Effect ............................................................... 91
Answcr .......................................................................................................... 92
Programming Exercises ................................................................................. 93

7. Programming Process ........................................................... 95


Setting Up ...................................................................................................... 97
Specification .................................................................................................. 98
Code Design .................................................................................................. 98
Prototype ....................................................................................................... 99
Makefilc ....................................................................................................... 101
Testing ......................................................................................................... 104
Debugging ................................................................................................... 104
Maintenance ................................................................................................ 107
Revisions ...................................................................................................... 107
Elcctronic Archacology ............................................................................... 107
Marking Up the Program ............................................................................ 108
Using the Debugger .................................................................................... 108
Text Editor as a Browser ............................................................................ 108
Add Comments ............................................................................................ 109
Programming Exercises ............................................................................... 111
v ii i Table of Contents

H. Simple Programming....................................................... 113

8. More Control Statements ................................................. 115


for Statement ............................................................................................... 115
switch Statement ......................................................................................... 118
switch, break, and continue ........................................................................ 123
Answers ....................................................................................................... 124
Programming Exercises ................................................................................ 125

9. Variable Scope and Functions ........................................ 127


Scope and Class ........................................................................................... 127
Functions ...................................................................................................... 130
Functions with No Parameters .................................................................... 134
Structured Programming .............................................................................. 135
Recursion ...................................................................................................... 136
Answers ....................................................................................................... 138
Programming Exercises ................................................................................ 138

10. C Preprocessor ................................................................. 140


#define Statement ......................................................................................... 140
Conditional Compilation .............................................................................. 146
include Files ................................................................................................. 148
Parameterized Macros .................................................................................. 150
Advanced Features ....................................................................................... 152
Summary ....................................................................................................... 152
Answers ........................................................................................................ 152
Programming Exercises ................................................................................ 154

11. Bit Operations ................................................................. 156


Bit Operators ................................................................................................ 158
The and Operator (&) .................................................................................. 158
Bitavise or ( I ) ............................................................................................... 160
The Bitwise Exclusive or (A) ....................................................................... 161
The Ones Complement Operator (Not) (—) ................................................ 161
The Left- and Right-Shift Operators (<<, ») .............................................. 162
Setting, Clearing, and Testing Bits ............................................................... 163
Bitmapped Graphics .................................................................................... 166
Table of Contents ix

Answers ....................................................................................................... 172


Programming Exercises ............................................................................... 172

12. Advanced Types ............................................................... 173


Structures ..................................................................................................... 173
Unions ........................................................................................................ 175
typedef ........................................................................................................ 177
enum Type .................................................................................................. 178
Casting ........................................................................................................ 179
Bit Fields or Packed Structures ................................................................... 179
Arrays of Structures ..................................................................................... 181
Summary ..................................................................................................... 182
Programming Exercises ............................................................................... 182

13. Simple Pointers ............................................................... 183


Pointers as Function Arguments ................................................................. 188
tonst Pointers ............................................................................................. 189
Pointers and Arrays ..................................................................................... 191
How Not to Use Pointers ............................................................................ 195
Using Pointers to Split a String ................................................................... 197
Pointers and Structures ............................................................................... 200
Command-Line Arguments ......................................................................... 201
Programming Exercises ............................................................................... 206
Answers ...................................................................................................... 206

14. File Input/Output............................................................ 209


Conversion Routines ................................................................................... 212
Binary and ASCII Files ................................................................................ 215
The End-of-Line Puzzle .............................................................................. 216
Binary ................................................................................................... 218
Buffering Problems ..................................................................................... 219
Unbuffered I/O ........................................................................................... 220
Designing File Formats ............................................................................... 224
Answers ....................................................................................................... 226
Programming Exercises ............................................................................... 227
x Table of Contents

15. Debugging and Optimization ........................................ 229


Debugging ............................................................................................................. 229
Interactive Debuggers .......................................................................................... 240
Debugging a I3inary Search ................................................................................ 244
Runtime Errors ...................................................................................................... 254
The Confessional Method of Debugging .......................................................... 255
Optimization .......................................................................................................... 256
Answcrs .................................................................................................................. 264
Programming Exercises ........................................................................................ 264

16 Floating Point................................................................... 265


Floating-Point Format .......................................................................................... 265
Floating Addition/Suhtraction ............................................................................ 266
Multiplication ......................................................................................................... 267
Division ................................................................................................................... 268
Overflow and Undcrflow ..................................................................................... 268
Roundoff Error ...................................................................................................... 269
Accuracy ................................................................................................................. 269
Minimizing Roundoff Error ................................................................................. 270
Detcrmining Accuracy ......................................................................................... 270
Precision and Speed ............................................................................................ 272
Power Series .......................................................................................................... 272
Programming Exercises ........................................................................................ 275

III. Advanced Programming Concepts................................. 277

17. Advanced Pointers ........................................................... 279


Pointers and Structures ........................................................................................ 279
free Function ......................................................................................................... 282
Linked List .............................................................................................................. 283
Structure Pointer Operator .................................................................................. 286
Ordered Linked Lists ............................................................................................ 287
Double-Linked Lists ............................................................................................. 288
Trees ....................................................................................................................... 292
Printing a Trce ....................................................................................................... 296
Rest of Program .....................................................................................................
296
I)ata Stnictures for a Chess Program ................................................................ 300
Table of Contents xt

Answers ...................................................................................................... 302


Programming Exercises ............................................................................... 303

18. Modular Programming ................................................. 304


Modules ...................................................................................................... 304
Public and Private ....................................................................................... 305
The extern Modifier .................................................................................... 306
Headers ....................................................................................................... 308
The Body of the Module ............................................................................ 310
A Program to Use Infinite Arrays ............................................................... 310
The Makefile for Multiple Files .................................................................. 313
Using the Infinite Array .............................................................................. 316
Dividing a Task into Modules .................................................................... 322
Module Division Example: Text Editor ....................................................... 323
Compiler ...................................................................................................... 324
Spreadsheet ................................................................................................. 325
Module Design Guidelines ......................................................................... 327
Programming Exercises ............................................................................... 328

19. Ancient Compilers .......................................................... 329


K&R-Style Functions ................................................................................... 329
Library Changes ........................................................................................... 332
Missing Features .......................................................................................... 333
Free/Malloc Changes .................................................................................. 333
lint ................. 334
Answers ...................................................................................................... 334

20. Portability Problems ....................................................... 337


Modularity .................................................................................................. 337
Word Sire ................................................................................................... 338
Byte Order Problem .................................................................................... 338
Alignment Problem ..................................................................................... 339
NULL Pointer Problem ................................................................................ 341
Filename Problems ...................................................................................... 341
File Types ................................................................................................... 342
Summary ..................................................................................................... 342
Answers ...................................................................................................... 343
xii Table of Contents

21. C's Dustier Corners .......................................................... 344


do/while ................................................................................................................. 344
goto .......................................................................................................................... 344
The ?: Construct ................................................................................................... 346
The , Operator ....................................................................................................... 346
volatile Qualifier .................................................................................................... 346
Answer .................................................................................................................... 347

22. Putting It All Together .................................................... 348


Requirements ......................................................................................................... 348
Specification .......................................................................................................... 348
Code Design .......................................................................................................... 350
Coding .................................................................................................................... 355
Functional Description ......................................................................................... 355
Expandability ......................................................................................................... 357
Testing .................................................................................................................... 358
Revisions ................................................................................................................ 359
A Final Warning .................................................................................................... 359
Program Files ......................................................................................................... 359
Programming Exercises ........................................................................................ 381

23. Programming Adages ..................................................... 382


General ................................................................................................................... 382
Design ..................................................................................................................... 383
Declarations ........................................................................................................... 383
switch Statement ................................................................................................... 383
Preprocessor .......................................................................................................... 384
Style ........................................................................................................................ 384
Compiling .............................................................................................................. 384
Final Note .............................................................................................................. 384
Answer .................................................................................................................... 385

IV Other Language Features ................................................ 387


A. ASCH Table ....................................................................... 389

B. Ranges and Parameter Passing Conversions ................. 391


Table of Contents xiti

C. Operator Precedence Rules ................................................ 393

D. A Program to Compute a Sine Using a Power Series .... 395

Glossary.................................................................................. 399

Index ...................................................................................... 421

Das könnte Ihnen auch gefallen