12
Practical C Programmin g Steve Oualline

Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

Practical C Programming

Steve Oualline

Page 2: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists
Page 3: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

Preface xviii

Scope of This Handbook xixConventions Used in This Handbook xx iAcknowledgments xxii

Chapter 1 The Basics of Program Writing 1

Text Editor 2Compiler 3The Library 3The Linker 3make Utility 5Debugger 5Wrappers and Integrated Development Environments 5

A Simple Example 6Getting Help 7

Chapter 2 Style 9

Common Coding Practices 14Coding Religion 1 6Indentation and Code Format 1 6Clarity 1 7Simplicity 1 8

Summary

J *f

Page 4: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

Chapter 3 Basic Declarations and Expressions + 20

Elements of a Program :.

20Basic Program Structure : 21Simple Expressions ::: ...+ :. .•:N : 2 3

BELVii-Nt k än torage ::; ~,. ..K 24Variable Declarations 2 5Integers r. . . 2 6Assignment' Statements 26printf Function . . .

. 2 8Floating Point x, 29

Floating Point Versus Integer Divide 30haracters r 32

Answers 3 3Programming Exercises 34

Chapter 4 Arrays, Qualifiers, and Reading Numbers 3 5

Arrays 35Strings 37

r''Reading Strings 40Multiple Dimensional Arrays 43Reading Numbers 44Initializing Variables 46Types of Integers 48Types of Floats 49Hexadecimal and Octal Constants 50Operators for Performing Shortcuts 5 1

Side Effects 52Answers 55Programming Exercises . .

. 56

Chapter 5 Decision and Control Statements 5 7

if Statement 58._l e_Statement

59How . of to Use strcm p

i • •pin tatements

60.

Page 5: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

while Statement 6 1break Statement 63continue Statement 65

The Assignment Anywhere Side Effect 66Answers 67

Programming Problems 6 7

Chapter 6 The Programming Process 69

Setting Up 7 1The Specification 72Code Design 74

The Prototype 74The Makefile 76

Testing ~ 77Debugging 78Maintenance 80Revisions 8 1Electronic Archaeology 8 1

Marking Up the Program 82Using the Debugger 82 _Text Editor as a Browser 82Add Comments 8 3Programming Exercises 86

Chapter 7 More Control Statements 87

for Statement 87switch Statement 91switch, break, and continue 96

Answers „ 98Programming Exercises x.98

7.

r'

Page 6: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

Chapter 8 Variable Scope and Functions 1 '100

Scope and Class 100Functions 10 4

Older-style K&R Function Declarations 10 8Functions With No Parameters 108

Structured Programming 110Recursion 112

Answers 11 3Programming Exercises 114

Chapter 9 The C Preprocessor 11 5

#define Statement 11 6Conditional Compilation 12 1Include Files 12 3Parameterized Macros 125Advanced Features 126Summary 12 7

Answers 12 7Programming Exercises 13 0

Chapter 10 Bit Operations 13 1

Bit Operators 132The and Operator 13 3Bitwise or 135The Bitwise Exclusive or 135The Ones Complement Operator (not) 136The Left and Right Shift Operators 136Setting, Clearing, and Testing Bits 13 7

Bitmapped Graphics 14 0Answers 144Programming Exercises 144

Page 7: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

Chapter 11 Advanced Types 146

Structures 146Unions 149typedef 15 1enum Type 152Bit Fields or Packed Structures 15 3Arrays of Structures 15 5Summary 15 6

Programming Exercises 15 6

Chapter 12 Simple Pointers 15 8

Pointers and Arrays 16 7Splitting 17 1Pointers and Structures 174Command-line Arguments 17 5

Answers 180Programming Problems 18 1

Chapter 13 File Input/Output 182

Conversion Routines 185Binary and ASCII Files 18 8

The End-of-line Puzzle 18 9Binary I/O 19 1Buffering Problems 192Unbuffered I/O 192Designing File Formats 197

Answers 19 9Programming Problems 200

Page 8: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

Chapter 14 Debugging and Optimization 201

Debugging 20 1Divide and Conquer 210Debug Only Code 210Debug Command-line Switch 210Going Through the Output 212Interactive Debuggers 212Debugging a Binary Search 216Runtime Errors 226The Confessional Method of Debugging 228

Optimization 229The Power of Powers of 2 23 1How to Optimize 234Case Study : Macros Versus Functions 236Case Study : Optimizing a Color Rendering Algorithm 236

Answers 237Programming Problems 237

Chapter 15 Floating Point 238

Floating-point Format 239Floating Addition/Subtraction 240Multiplication 24 1Division 24 1Overflow and Underflow 242Roundoff Error 242

Accuracy 243Minimizing Roundoff Error 244

Accuracy 244Precision and Speed 246Power Series 24 7Programming Problems 249

Page 9: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

Chapter 16 Advanced Pointers 250

Pointers and Structures : 25 1free Function 25 4

Linked List 25 5Structure Pointer Operator 259Ordered Linked Lists 259Double-linked List 262Trees 267

Printing a Tree 270The Rest of the Program 27 1

Data Structures for a Chess Program 275Answers 277

Programming Problems 279

Chapter 17 Modular Programming 280

Modules 280Public and Private 281The extern Modifier 28 1

Headers 284The Body of the Module 286

A Program to Use Infinite Arrays 286The Makefile for Multiple Files 289

Using the Infinite Array 293Dividing a Task Up into Modules 298

Module Division Example : Text Editor 299Compiler 30 1

Spreadsheet 303

Module Design Guidelines 305

Programming Problems 305

Chapter 18 Portability Problems 306

- 11II-Modularity

30. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Word Size r . .e_'F30 7Byte Order Problem 30 8

Alignment Problem 309

Page 10: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

NULL Pointer Problem 310Filename Problems 31 1File Types r 312Summary 312

Answers 312

Chapter 19 C's Dustier Comers 314

do/while 314goto 31 5The ? : Construct 316The, Operator 316

Answers 317

Chapter 20 Putting It All Together 318

Assignment 31 8Specification 31 9Code Design 32 1

Main Module 32 1Macro Module 322Font Module 322Symbol Table Module 322

Coding 323Functional Description 323

tlint.c 324macros .c 324font.c 325symbol .c 325

Testing 326Revisions 32 7Program Files 32 7

The gen .h File 32 7The font .h File 32 8The macro .h File 32 8The tlint .c File 329The macros.c File 33 1. . . . . . . . . . . . . .. . . . . . . . . .. . . . ..The fonts .c File 33 9The symbol .c File 340

Page 11: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

UNIX Makefile : 342Turbo C Makefile 342The standard .mac File 343The standard .fonts File 345Test Script 345The troff.test File 346The Bad .mac File 347The Two.font File 347

Programming Problems 347

Chapter 21 Programming Adages 348{ r. ... R

General . . ..,- 348Design 349Declarations 349switch Statement 350Preprocessor 350Style 35 1Compiling 351Final Note 35 1

Answers

Appendix A ASCII Chart 353

Appendix B Numeric Limits 35 6

Ranges 35 6

Appendix C Operator Precedence Rules 358

ANSI Standard Rules 358Practical Subset ,.ti 359

Page 12: Practical C Programming - GBV · Chapter 16 Advanced Pointers 250 Pointers and Structures : 25 1 free Function 254 Linked List 255 Structure Pointer Operator 259 Ordered Linked Lists

_Appendix D Program to Compute sine Using a Power Series 360

Makefile . ::::;.:.a .::.:..::.:..: . : ...:: . . ..~. ..:.. b : 36 1The sine.c Program 36 1

Appendix E Automatic Type Conversion Used When Passing Parameters . . . 365

Glossary 366

Index 388