Bradley L Jones Peter Aitken Dean Miller Sa ms Teach Yourself C Programming in One Hour a Day Seventh Edition 800 East 96th Street, Indianapolis, Indiana 46240
Table of Contents Introduction part I: Fundamentals of C LESSON l: Getting Started with C 5 A Brief History of the C Language 6 Why Use C? 6 Preparing to Program 8 The Program Development Cycle 8 Creating the Source Code 9 Compiling the Source Code 9 Linking to Create an Executable File 10 Completing the Development Cycle 11 Your First C Program 13 Entering and Compiling helloc 14 Summary 18 Q&A 18 Workshop 19 Quiz 19 Exercises 20 lesson 2: The Components of a C Program 23 A Short C Program 24 The Program's Components 25 The main () Function (Lines 9 Through 23) 25 The ((include and #define Directives (Lines 2 and 3) 25 The Variable Definition (Line 5) 26 The Function Prototype (Line 7) 26 Program Statements (Lines 12 13, 14 17 19 20 22 and 28) 26 The Function Definition (Lines 26 Through 29) 27 Program Comments (Lines 1 II 16 and 25) 27 Using Braces (Lines 10, 23, 27 and 29) 29 Running the Program 29 A Note on Accuracy 29
Contents i v A Review of the Parts of a Program 30 Summary 32 Q&A 33 Workshop 33 Quiz 33 Exercises 34 lesson 3: Storing Information: Variables and Constants 37 Understanding Your Computer's Memory 38 Storing Information with Variables 39 Variable Names 39 Numeric Variable Types 40 Variable Declarations 44 The typedef Keyword 45 Initializing Variables 45 Constants 47 Literal Constants 47 Symbolic Constants 48 Summary 53 Q&A 53 Workshop 54 Quiz 54 Exercises 55 lesson 4: The Pieces of a C Program: Statements, Expressions, and Operators 57 Statements 58 The Impact of White Space on Statements 58 Creating a Null Statement 59 Working with Compound Statements 59 Understanding Expressions 60 Simple Expressions 60 Complex Expressions 60 Operators 61 The Assignment Operator 62 The Mathematical Operators 62 Operator Precedence and Parentheses 67
vi Sams Teach Yourself C Programming in One Hour a Day Order of Subexpression Evaluation 69 The Relational Operators 70 The if Statement 71 The else Clause 74 Evaluating Relational Expressions 77 The Precedence of Relational Operators 79 The Logical Operators 80 More on True/False Values 82 The Precedence of Operators 82 Compound Assignment Operators 84 The Conditional Operator 85 The Comma Operator 85 Operator Precedence Revisited 86 Summary 87 Q&A 88 Workshop 88 Quiz 89 Exercises 89 LESSON 5: Packaging Code in Functions 91 Understanding Functions 92 A Function Defined 92 A Function Illustrated 92 How a Function Works 95 Functions and Structured Programming 97 The Advantages of Structured Programming 97 Planning a Structured Program 97 The Top-Down Approach 99 Writing a Function 100 The Function Header 100 The Function Body 103 The Function Prototype 109 Passing Arguments to a Function 110 Calling Functions 110 Recursion 1 \ 2 Where the Functions Belong 114
while Contents j vii Working with Inline Functions 115 Summary 115 Q&A 116 Workshop 116 Quiz 116 Exercises 117 LESSON 6: Basic Program Control 119 Arrays: The Basics 120 Controlling Program Execution 121 The for Statement 121 Nesting for Statements 127 The while Statement 130 Nesting while Statements 134 The do Loop 136 Nested Loops 141 Summary 142 Q&A 142 Workshop 143 Quiz 143 Exercises 143 lesson 7: Fundamentals of Reading and Writing Information 145 Displaying Information Onscreen 146 TheprintfO Function 146 TheprintfO Format Strings 146 Displaying Messages with puts () 155 Inputting Numeric Data with scanf () 156 163 Using Trigraph Sequences 161 Summary 162 Q&A Workshop 163 Quiz 163 Exercises 164
viii Sams Teach Yourself C Programming in One Hour a Day part II: Putting C to Work LESSON 8: Using Numeric Arrays 167 What Is an Array? 168 Using Single-Dimensional Arrays 169 Using Multidimensional Arrays 173 Naming and Declaring Arrays 174 Initializing Arrays 178 Initializing Multidimensional Arrays 179 Summary 182 Q&A 183 Workshop 184 Quiz 184 Exercises 184 LESSON 9: Understanding Pointers 187 What Is a Pointer? 188 Your Computer's Memory 188 Creating a Pointer 188 Pointers and Simple Variables 189 Declaring Pointers 189 Initializing Pointers 190 Using Pointers 190 Pointers and Variable Types 193 Pointers and Arrays 194 The Array Name as a Pointer 194 Array Element Storage 195 Pointer Arithmetic 198 Pointer Cautions 202 Array Subscript Notation and Pointers 203 Passing Arrays to Functions 204 Summary 209 Q&A 210 Workshop 210 Quiz 210 Exercises 211
Contents j ix LESSON 10: Working with Characters and Strings 213 The char Data Type 214 Using Character Variables 215 Using Strings 218 Arrays of Characters 218 Initializing Character Arrays 219 Strings and Pointers 219 Strings Without Arrays 220 Allocating String Space at Compilation 220 The malloc () Function 221 Using the malloc () Function 222 Displaying Strings and Characters 226 The puts () Function 226 Theprintf () Function 227 Reading Strings from the Keyboard 228 Inputting Strings Using the gets () Function 228 Inputting Strings Using the scanf () Function 232 Summary 235 Q&A 235 Workshop 237 Quiz 237 Exercises 238 lesson 11: Implementing Structures, Unions, and TypeDefs 241 Working with Simple Structures 242 Defining and Declaring Structures 242 Accessing Members of a Structure 243 Using Structures That Are More Complex 246 Including Structures Within Other Structures 246 Structures That Contain Arrays 250 Arrays of Structures 252 Initializing Structures 256 Structures and Pointers 259 Including Pointers as Structure Members 259 Creating Pointers to Structures 261 Working with Pointers and Arrays of Structures 264 Passing Structures as Arguments to Functions 267
X Sams Teach Yourself C Programming in One Hour a Day Understanding Unions 268 Defining Declaring, and Initializing Unions 269 Accessing Union Members 269 Creating Synonyms for Structures with typedef 274 Summary 275 Q&A 275 Workshop 276 Quiz 276 Exercises 277 LESSON 12: Understanding Variable Scope 279 What Is Scope? 280 A Demonstration of Scope 280 The Importance of Scope 282 Creating External Variables 282 External Variable Scope 283 When to Use External Variables 283 The extern Keyword 283 Creating Local Variables 285 Static Versus Automatic Variables 285 The Scope of Function Parameters 288 External Static Variables 289 Register Variables 289 Local Variables and the main () Function 290 Which Storage Class Should You Use? 291 Local Variables and Blocks Summary Q&A Workshop Quiz Exercises 291 293 293 294 294 295 LESSON 13: Advanced Program Control Ending Loops Early The break Statement The continue Statement 299 ^qq 3qq ^02
Contents xi The goto Statement 304 Infinite Loops 307 The switch Statement 311 Exiting the Program 320 The exit () Function 320 Summary 321 Q&A 321 Workshop 322 Quiz 322 Exercises 322 lesson 14: Working with the Screen, Printer, and Keyboard 325 Streams and C 326 What Exactly Is Program Input/Output'' 326 What Is a Stream17 326 Text Versus Binary Streams 327 Predefined Streams 327 Using C's Stream Functions 328 An Example 329 Accepting Keyboard Input 329 Character Input 330 Working with Formatted Input 338 Controlling Output to the Screen 347 Character Output with putchar (), putc (), and fputc () 347 Using puts 0 and fputs 0 for String Output 349 Using printf () andfprintfo for Formatted Output 350 When to Use fprintf () 357 Using stderr 357 Summary 358 Q&A 359 Workshop 359 Quiz 359 Exercises 360
xii Sams Teach Yourself C Programming in One Hour a Day part ill: Advanced C 383 LESSON 15: Pointers to Pointers and Arrays of Pointers 361 Declaring Pointers to Pointers 362 Pointers and Multidimensional Arrays 363 Working with Arrays of Pointers 372 Strings and Pointers: A Review 372 Declaring an Array of Pointers to Type char 373 Pulling Things Together with an Example 375 Summary 381 Q&A 382 Workshop 382 Quiz 382 Exercises LESSON 16: Pointers to Functions and Linked Lists 385 Working with Pointers to Functions 386 Declaring a Pointer to a Function 386 Initializing and Using a Pointer to a Function 387 Understanding Linked Lists 396 Basics of Linked Lists 396 Working with Linked Lists 398 A Simple Linked List Demonstration 403 Implementing a Linked List 406 Summary 415 Q&A 415 Workshop 415 Quiz 415 Exercises 416 LESSON 17: Using Disk Files 417 Relating Streams to Disk Files 418 Understanding the Types of Disk Files 418 Using Filenames 41 g Opening a File 419
j Contents xiii Writing and Reading File Data 423 Formatted File Input and Output 424 Character Input and Output 428 Direct File Input and Output 431 File Buffering: Closing and Flushing Files 435 Understanding Sequential Versus Random File Access 436 The f tell () andrewindo Functions 437 The f seek () Function 440 Detecting the End of a File 443 File Management Functions 445 Deleting a File 445 a Renaming File 446 Copying a File 447 Using Temporary Files 450 Summary 452 Q&A 452 Workshop 453 Quiz 453 Exercises 454 LESSON 18: Manipulating Strings 455 Determining String Length 456 Copying Strings 457 ThestrcpyO Function 457 The strncpyo Function 459 Concatenating Strings 461 Using the strcat () Function 461 Using the strncat () Function 462 Comparing Strings 464 Comparing Two Entire Strings 464 Comparing Partial Strings 466 Searching Strings 468 ThestrchrO Function 468 The strrchr () Function 470 The strcspn () Function 470 ThestrspnO Function 471
xiv Sams Teach Yourself C Programming in One Hour a Day The strpbrko Function 473 The strstro Function 473 String-to-Number Conversions 474 Converting Strings to Integers 475 Converting Strings to Longs 475 Converting Strings to Long Longs 476 Converting Strings to Floating-Point Numeric Values 476 Character-Test Functions 477 ANSI Support for Uppercase and Lowercase 481 Summary 483 Q&A 483 Workshop 484 Quiz 484 Exercises 484 lesson 19: Getting More from Functions 487 Passing Pointers to Functions 488 Type void Pointers 492 Using Functions That Have a Variable Number of Arguments 496 Functions That Return a Pointer 499 Summary 501 Q&A 502 Workshop 502 Quiz 502 Exercises 503 LESSON 20: Exploring the C Function Library 505 Mathematical Functions 506 Trigonometric Functions 506 Exponential and Logarithmic Functions 506 Hyperbolic Functions 507 Other Mathematical Functions 507 A Demonstration of the Math Functions 508 Dealing with Time 509 Representing Time 509 The Time Functions 510 Using the Time Functions 513
Contents xv Error-Handling 516 The assert () Macro 516 The errno h Header File 518 530 TheperrorO Function 519 Searching and Sorting 521 Searching with bsearch () 521 Sorting with qsort () 523 Searching and Sorting: Two Demonstrations 523 Summary 529 Q&A 529 Workshop Quiz 530 Exercises 531 LESSON 21: Working with Memory 533 Type Conversions 534 Automatic Type Conversions 534 Explicit Conversions Using Typecasts 536 Allocating Memory Storage Space 538 Allocating Memory with the malloc () Function 539 Allocating Memory with the calloc () Function 540 Allocating More Memory with the realloc () Function 541 Releasing Memory with the free () Function 543 Manipulating Memory Blocks 545 Initializing Memory with the memset () Function 545 Copying Memory with the memcpy () Function 546 Moving Memory with the memmove () Function 546 Working with Bits 548 The Shift Operators 548 The Bitwise Logical Operators 550 The Complement Operator 552 Bit Fields in Structures 552 Summary 554 Q&A 554 Workshop 556 Quiz 556 Exercises 557
xvi Sams Teach Yourself C Programming in One Hour a Day lesson 22: Advanced Compiler Use 559 Programming with Multiple Source-Code Files 560 Advantages of Modular Programming 560 Modular Programming Techniques 560 Module Components 564 External Variables and Modular Programming 565 The C Preprocessor 567 The #def ine Preprocessor Directive 567 Using the #inciude Directive 572 Using Using #if ttif #elif tfelse, and tfendif 573 #endif to Help Debug 574 Avoiding Multiple Inclusions of Header Files 575 The tfundef Directive 576 Predefined Macros 576 Using Command-Line Arguments 577 Summary 580 Q&A 580 Workshop 581 Quiz 581 Exercises 582 part 4: Appendixes APPENDIX A: ASCII Chart 583 APPENDIX B: C/C++ Reserved Words 589 APPENDIX C: Common C Functions 593 appendix D: Answers 599 Index 645