Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Practical C Programming Third Edition pdf
PREMIUM
Số trang
456
Kích thước
10.3 MB
Định dạng
PDF
Lượt xem
725

Tài liệu Practical C Programming Third Edition pdf

Nội dung xem thử

Mô tả chi tiết

Practical C Programming

Practical C Programming

Third Edition

Steve Oualline

Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo

Practical C Programming, Third Edition

by Steve Oualline

Copyright © 1997, 1993, 1991 O’Reilly Media, Inc. All rights reserved.

Printed in the United States of America.

Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

Editor, First and Second Editions: Tim O’Reilly

Editor, Third Edition: Andy Oram

Production Editor: Nicole Gipson Arigo

Printing History:

July 1991: First Edition.

August 1992: Minor corrections.

January 1993: Second Edition. Bugs fixed; programs all now conform to ANSI C.

June 1993: Minor corrections.

August 1997: Third Edition.

Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered

trademarks and The Java Series is a trademark of O’Reilly Media, Inc. Practical C

Programming, Third Edition, the image of a cow, and related trade dress are trademarks of

O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish

their products are claimed as trademarks. Where those designations appear in this book, and

O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in

caps or initial caps.

While every precaution has been taken in the preparation of this book, the publisher assumes

no responsibility for errors or omissions, or for damages resulting from the use of the

information contained herein.

ISBN: 978-1-565-92306-5

[LSI] [2011-02-02]

v

Table of Contents

Preface .............................................................................................. xv

How This Book is Organized .........................................................................xvi

Chapter by Chapter .........................................................................................xvi

Notes on the Third Edition .............................................................................xix

Font Conventions ............................................................................................xix

Obtaining Source Code ....................................................................................xx

Comments and Questions .............................................................................xxii

Acknowledgments ..........................................................................................xxii

Acknowledgments to the Third Edition .......................................................xxiii

I. 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 Help in an Integrated Development Environment ...........................34

IDE Cookbooks ................................................................................................34

Programming Exercises ....................................................................................37

3. Style ..................................................................................................38

Common Coding Practices ..............................................................................42

Coding Religion ................................................................................................45

Indentation and Code Format .........................................................................45

Clarity ................................................................................................................46

Simplicity ..........................................................................................................47

Summary ...........................................................................................................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 Divide ..............................................................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

Initializing Variables .........................................................................................73

Types of Integers .............................................................................................75

Types of Floats .................................................................................................77

Constant Declarations ......................................................................................77

Table of Contents vii

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

continue Statement ..........................................................................................90

Assignment Anywhere Side Effect ..................................................................91

Answer ..............................................................................................................92

Programming Exercises ....................................................................................93

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

Setting Up .........................................................................................................97

Specification .....................................................................................................98

Code Design .....................................................................................................98

Prototype ..........................................................................................................99

Makefile ..........................................................................................................101

Testing ............................................................................................................104

Debugging ......................................................................................................104

Maintenance ...................................................................................................107

Revisions .........................................................................................................107

Electronic Archaeology ..................................................................................107

Marking Up the Program ...............................................................................108

Using the Debugger .......................................................................................108

Text Editor as a Browser ...............................................................................108

Add Comments ...............................................................................................109

Programming Exercises ..................................................................................111

viii Table of Contents

II. 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

Bitwise or (|) .................................................................................................160

The Bitwise Exclusive or (^) .........................................................................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

const 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 I/O ......................................................................................................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 Binary Search ...........................................................................244

Runtime Errors ...............................................................................................254

The Confessional Method of Debugging ......................................................255

Optimization ...................................................................................................256

Answers ..........................................................................................................264

Programming Exercises ..................................................................................264

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

Floating-Point Format ....................................................................................265

Floating Addition/Subtraction .......................................................................266

Multiplication ..................................................................................................267

Division ...........................................................................................................268

Overflow and Underflow ...............................................................................268

Roundoff Error ...............................................................................................269

Accuracy .........................................................................................................269

Minimizing Roundoff Error ............................................................................270

Determining 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 Tree ................................................................................................296

Rest of Program ..............................................................................................296

Data Structures for a Chess Program ............................................................300

Table of Contents xi

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 Size .......................................................................................................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. ASCII Table ................................................................................... 389

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

Table of Contents xiii

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

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

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

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

Tải ngay đi em, còn do dự, trời tối mất!