applied introductory c programming - university of new...

30
Applied Introductory C Programming Alice E. Fischer David W. Eggert University of New Haven Michael J. Fischer Yale University August 2011

Upload: others

Post on 30-Jul-2020

3 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

Applied Introductory C Programming

Alice E. Fischer

David W. Eggert

University of New Haven

Michael J. Fischer

Yale University

August 2011

Page 2: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

ii

Copyright c©1995, 1996, 1997, 1998, 1999, 2004, 2011

by Alice E. Fischer, David W. Eggert, and Michael J. Fischer

All rights reserved. No part of this manuscript may be reproduced, stored in a retrieval system, ortransmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise,without the prior written permission of the authors.

Page 3: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

Contents

Preface v

1 Computers and Systems 31.1 The Physical Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Fig. 1.1: Basic architecture of a modern computer. . . . . . . . . . . . . . . . . . 31.1.1 The Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.1.2 The Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Fig. 1.2: Main memory in a byte-addressable machine. . . . . . . . . . . . . . . . 5Fig. 1.3: The memory hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1.1.3 Input and Output Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71.1.4 The Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71.1.5 Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Fig. 1.4: A local network in a student lab. . . . . . . . . . . . . . . . . . . . . . . 81.2 The Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91.3 Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.3.1 Machine Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101.3.2 Assembly Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111.3.3 High-Level Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.4 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131.4.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131.4.2 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

1.5 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141.5.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141.5.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2 Programs and Programming 152.1 What Is a Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Fig. 2.1: A simple algorithm: Find an average. . . . . . . . . . . . . . . . . . . . . 152.1.1 The Simplest Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Fig. 2.2: A simple program: Hail and farewell. . . . . . . . . . . . . . . . . . . . . 162.2 The stages of program development. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.2.1 Define the Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Fig. 2.3: Problem specification: Find the average exam score. . . . . . . . . . . . 18

2.2.2 Design a Test Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Fig. 2.4: Test plan for exam average program. . . . . . . . . . . . . . . . . . . . . 18

2.2.3 Design a Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Fig. 2.5: Pseudocode program sketch: Find an average. . . . . . . . . . . . . . . . 19

2.3 The Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192.3.1 The Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Fig. 2.6: The stages of program translation. . . . . . . . . . . . . . . . . . . . . . 20

iii

Page 4: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

iv CONTENTS

2.3.2 The Translator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202.3.3 Linkers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2.4 Source Code Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212.4.1 Create a Source File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212.4.2 Coding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Fig. 2.7: A C program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222.4.3 Translate the Program and Correct Errors . . . . . . . . . . . . . . . . . . . . . . . . . 23

2.5 Program Execution and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242.5.1 Execute the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

Fig. 2.8: Memory for constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Fig. 2.9: Testing a program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Fig. 2.10: Memory during program execution. . . . . . . . . . . . . . . . . . . . . . 25

2.5.2 Test and Verify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252.6 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2.6.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262.6.2 The moral of this story. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272.6.3 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

2.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282.7.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282.7.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282.7.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

3 The Core of C 313.1 The Process of Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Fig. 3.1: The stages of compilation. . . . . . . . . . . . . . . . . . . . . . . . . . . 313.2 The Parts of a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

3.2.1 Terminology. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Fig. 3.2: How to lay out a simple program. . . . . . . . . . . . . . . . . . . . . . . 32

3.2.2 The main() program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Fig. 3.3: Words in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

3.3 An Overview of Variables, Constants, and Expressions . . . . . . . . . . . . . . . . . . . . . . 343.3.1 Values and Storage Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.3.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

Fig. 3.4: Syntax for declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Fig. 3.5: Simple declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

3.3.3 Constants and Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Fig. 3.6: Using constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

3.3.4 Names and Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393.3.5 Arithmetic and Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

3.4 Simple Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403.4.1 Formats. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

3.5 A Program with Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Fig. 3.7: Grapefruits and gravity. . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

3.6 The Flow of Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Fig. 3.8: A complete flow diagram of a simple program. . . . . . . . . . . . . . . 45Fig. 3.9: Diagram of the grapefruits and gravity program. . . . . . . . . . . . . . 45

3.7 Asking Questions: Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453.7.1 The Simple if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

Fig. 3.10: Asking a question. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463.7.2 The if...else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

Fig. 3.11: Testing the limits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Fig. 3.12: Flow diagram for Testing the limits. . . . . . . . . . . . . . . . . . . . . 48

Page 5: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS v

3.7.3 Which if Should Be Used? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503.7.4 Options for Syntax and Layout (Optional Topic) . . . . . . . . . . . . . . . . . . . . . 51

Fig. 3.13: The if statement with and without braces. . . . . . . . . . . . . . . . . 513.8 Loops and Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

3.8.1 A Counting Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Fig. 3.14: Countdown. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

3.8.2 An Input Data Validation Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54Fig. 3.15: Input validation using while. . . . . . . . . . . . . . . . . . . . . . . . . 54

3.9 An Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Fig. 3.16: Problem specification: Gas prices. . . . . . . . . . . . . . . . . . . . . . . 56Fig. 3.17: Test plan: Gas prices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Fig. 3.18: Problem solution: Gas prices. . . . . . . . . . . . . . . . . . . . . . . . . 58

3.10 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593.10.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593.10.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603.10.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613.10.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613.10.5 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

3.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633.11.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633.11.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643.11.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

Fig. 3.19: Sketch for the heat transfer program. . . . . . . . . . . . . . . . . . . . . 66Fig. 3.20: Sketch for the temperature conversion program. . . . . . . . . . . . . . . 66Fig. 3.21: Problem specification: Gasket area. . . . . . . . . . . . . . . . . . . . . . 67Fig. 3.22: Flow diagram for the gasket area program. . . . . . . . . . . . . . . . . . 67

4 Expressions 734.1 Expressions and Parse Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

4.1.1 Operators and Arity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734.1.2 Precedence and Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

Fig. 4.1: Arithmetic operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744.1.3 Parsing and Parse Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

Fig. 4.2: Applying precedence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Fig. 4.3: Applying associativity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Fig. 4.4: Applying the parsing rules. . . . . . . . . . . . . . . . . . . . . . . . . . 76

4.2 Arithmetic, Assignment, and Combination Operators . . . . . . . . . . . . . . . . . . . . . . . 76Fig. 4.5: Assignment and arithmetic combinations. . . . . . . . . . . . . . . . . . 76Fig. 4.6: Parse tree for an assignment combination. . . . . . . . . . . . . . . . . . 76Fig. 4.7: Arithmetic combinations. . . . . . . . . . . . . . . . . . . . . . . . . . . 77

4.3 Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Fig. 4.8: Increment and decrement operators. . . . . . . . . . . . . . . . . . . . . 78

4.3.1 Parsing Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . 78Fig. 4.9: Increment and decrement trees. . . . . . . . . . . . . . . . . . . . . . . . 78

4.3.2 Prefix versus Postfix Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794.3.3 Mixing increment or decrement with other operators. . . . . . . . . . . . . . . . . . . . 80

Fig. 4.10: Evaluating an increment expression. . . . . . . . . . . . . . . . . . . . . 804.4 The sizeof operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

Fig. 4.11: The size of things. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814.5 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

Fig. 4.12: Relational operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824.5.1 True and False . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

Page 6: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

vi CONTENTS

4.5.2 The semantics of comparison. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834.6 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

4.6.1 Truth-valued operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83Fig. 4.13: Truth table for the logical operators. . . . . . . . . . . . . . . . . . . . . 83

4.6.2 Parse Trees for Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834.6.3 Lazy Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

Fig. 4.14: Precedence of the logical operators. . . . . . . . . . . . . . . . . . . . . . 84Fig. 4.15: Parsing logical operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . 84Fig. 4.16: Lazy truth table for logical–AND. . . . . . . . . . . . . . . . . . . . . . . 84Fig. 4.17: Lazy evaluation of logical–AND. . . . . . . . . . . . . . . . . . . . . . . . 84Fig. 4.18: Lazy truth table for logical–OR. . . . . . . . . . . . . . . . . . . . . . . . 84Fig. 4.19: Lazy evaluation of logical-OR. . . . . . . . . . . . . . . . . . . . . . . . . 86

4.7 Integer Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864.7.1 Integer Division and Modulus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

Fig. 4.20: The modulus operation is cyclic. . . . . . . . . . . . . . . . . . . . . . . 874.7.2 Applying Integer Division and Modulus . . . . . . . . . . . . . . . . . . . . . . . . . . 88

Fig. 4.21: Visualizing the modulus operator. . . . . . . . . . . . . . . . . . . . . . . 88Fig. 4.22: Positional notation and base conversion. . . . . . . . . . . . . . . . . . . 88Fig. 4.23: Number conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Fig. 4.24: A flow diagram for the base conversion. . . . . . . . . . . . . . . . . . . 88

4.8 Techniques for Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 914.8.1 Using Assignments and Printouts to Debug . . . . . . . . . . . . . . . . . . . . . . . . 91

Fig. 4.25: Problem specification: Ice cream for the picnic. . . . . . . . . . . . . . . 91Fig. 4.26: How much ice cream? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

4.8.2 Case Study: Using a Parse Tree to Debug . . . . . . . . . . . . . . . . . . . . . . . . . 93Fig. 4.27: Problem specification: Computing resistance. . . . . . . . . . . . . . . . 93Fig. 4.28: Computing resistance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Fig. 4.29: Finding an expression error. . . . . . . . . . . . . . . . . . . . . . . . . . 96Fig. 4.30: Parsing the corrected expression. . . . . . . . . . . . . . . . . . . . . . . 96

4.9 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974.9.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974.9.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 984.9.3 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

Fig. 4.31: Difficult aspects of C operators. . . . . . . . . . . . . . . . . . . . . . . . 984.9.4 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 984.9.5 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

4.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994.10.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994.10.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1014.10.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

5 Using Functions and Libraries 1055.1 Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

5.1.1 Standard Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065.1.2 Other Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065.1.3 Using Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

Fig. 5.1: A library is like an iceberg: only a small part is visible. . . . . . . . . . 106Fig. 5.2: Form of a simple function prototype. . . . . . . . . . . . . . . . . . . . 107

5.2 Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Fig. 5.3: Form of a simple function call. . . . . . . . . . . . . . . . . . . . . . . . . 108Fig. 5.4: Calling library functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

5.2.1 Call Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

Page 7: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS vii

Fig. 5.5: Call graph for Figure 5.4: Calling library functions. . . . . . . . . . . . 1115.2.2 Math Library Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

Fig. 5.6: Functions in the math library. . . . . . . . . . . . . . . . . . . . . . . . . 111Fig. 5.7: Rounding down: floor(). . . . . . . . . . . . . . . . . . . . . . . . . . . 112Fig. 5.8: Rounding up: ceil(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112Fig. 5.9: Rounding to the nearest integer: rint(). . . . . . . . . . . . . . . . . . . 113Fig. 5.10: Truncation via assignment. . . . . . . . . . . . . . . . . . . . . . . . . . . 113

5.2.3 Other Important Library Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113Fig. 5.11: A few functions in other libraries. . . . . . . . . . . . . . . . . . . . . . . 114

5.3 Programmer-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1145.3.1 Function syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1155.3.2 Defining Void→Void Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

Fig. 5.12: Using void→void functions. . . . . . . . . . . . . . . . . . . . . . . . . . 115Fig. 5.13: Printing a banner on your output. . . . . . . . . . . . . . . . . . . . . . . 115Fig. 5.14: A call graph for the beep program. . . . . . . . . . . . . . . . . . . . . . 115

5.3.3 Returning Results from a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185.3.4 Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185.3.5 Defining a Double→Double Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

Fig. 5.15: The grapefruit returns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Fig. 5.16: Call graph for the grapefruit returns. . . . . . . . . . . . . . . . . . . . . 121Fig. 5.17: Flow diagram for the grapefruit returns. . . . . . . . . . . . . . . . . . . 121Fig. 5.18: Definition of the drop() function. . . . . . . . . . . . . . . . . . . . . . . 121

5.4 Organization of a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Fig. 5.19: Functions, prototypes, and calls. . . . . . . . . . . . . . . . . . . . . . . 123Fig. 5.20: Function call graph with two levels of calls. . . . . . . . . . . . . . . . . 123Fig. 5.21: A function is a black box. . . . . . . . . . . . . . . . . . . . . . . . . . . 125

5.5 Application: Numerical Integration by Summing Rectangles . . . . . . . . . . . . . . . . . . . 126Fig. 5.22: The area under a curve. . . . . . . . . . . . . . . . . . . . . . . . . . . . 126Fig. 5.23: Integration by summing rectangles. . . . . . . . . . . . . . . . . . . . . . 126

5.6 Functions with More Than One Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1285.6.1 Function Syntax: Two Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128

Fig. 5.24: Using functions with two parameters and return values. . . . . . . . . . 129Fig. 5.25: Functions with two parameters and return values. . . . . . . . . . . . . . 129

5.7 Application: Generating “Random” Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 1315.7.1 Pseudo-Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1315.7.2 How Good Is the Standard Random Number Generator? . . . . . . . . . . . . . . . . 131

Fig. 5.26: Generating random numbers. . . . . . . . . . . . . . . . . . . . . . . . . 1325.8 Application: A Guessing Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

5.8.1 Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1345.8.2 Playing the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

Fig. 5.27: Halving the range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134Fig. 5.28: Can you guess my number? . . . . . . . . . . . . . . . . . . . . . . . . . 135Fig. 5.29: Guessing a number. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

5.9 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1385.9.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1385.9.2 Local Libraries and Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1385.9.3 The Order of the Parts of a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1405.9.4 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1405.9.5 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1415.9.6 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1415.9.7 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

5.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

Page 8: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

viii CONTENTS

5.10.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1425.10.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1435.10.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

6 More Repetition and Decisions 1476.1 New Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

6.1.1 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147Fig. 6.1: The for statement is a shorthand form of a while statement. . . . . . . 147Fig. 6.2: A simple program with a for statement. . . . . . . . . . . . . . . . . . . 147Fig. 6.3: A flow diagram for the for statement. . . . . . . . . . . . . . . . . . . . 148Fig. 6.4: Diagrams of corresponding while and for loops. . . . . . . . . . . . . . 148

6.1.2 The do...while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150Fig. 6.5: A flow diagram for the do...while statement. . . . . . . . . . . . . . . 150

6.1.3 Other Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150Fig. 6.6: Using continue with while and do. . . . . . . . . . . . . . . . . . . . . 151Fig. 6.7: Using continue with for. . . . . . . . . . . . . . . . . . . . . . . . . . . 151

6.1.4 Defective Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152Fig. 6.8: No update and no exit: Two defective loops. . . . . . . . . . . . . . . . . 152

6.2 Applications of Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1536.2.1 Sentinel Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

Fig. 6.9: A cash register program. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1546.2.2 Query Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

Fig. 6.10: Form of a query loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154Fig. 6.11: Repeating a calculation. . . . . . . . . . . . . . . . . . . . . . . . . . . . 156Fig. 6.12: The work() and drop() functions. . . . . . . . . . . . . . . . . . . . . . 156Fig. 6.13: Flow diagram for repeating a process. . . . . . . . . . . . . . . . . . . . 156

6.2.3 Counted Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158Fig. 6.14: Summing a series. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

6.2.4 Input Validation Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160Fig. 6.15: Input validation using a while statement. . . . . . . . . . . . . . . . . . 160

6.2.5 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161Fig. 6.16: Printing a table with nested for loops. . . . . . . . . . . . . . . . . . . 161Fig. 6.17: Flow diagram for the multiplication table program. . . . . . . . . . . . . 161

6.2.6 Delay Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Fig. 6.18: Using a delay loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Fig. 6.19: Delaying progress, the delay() function. . . . . . . . . . . . . . . . . . . 163

6.2.7 Flexible-Exit Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164Fig. 6.20: A structured loop with break. . . . . . . . . . . . . . . . . . . . . . . . 164Fig. 6.21: Input validation loop using a for statement. . . . . . . . . . . . . . . . . 165

6.2.8 Counted Sentinel Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166Fig. 6.22: Skeleton of a counted sentinel loop. . . . . . . . . . . . . . . . . . . . . . 166Fig. 6.23: Breaking out of a loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . 166Fig. 6.24: Avoiding a break-out. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

6.2.9 Search Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166Fig. 6.25: Skeleton of a search loop. . . . . . . . . . . . . . . . . . . . . . . . . . . 168

6.3 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1686.3.1 Syntax and Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

Fig. 6.26: Problem specification: Wire gauge adequacy evaluation. . . . . . . . . . 168Fig. 6.27: Diagrams for a nested conditional and a corresponding switch. . . . . . 169

6.3.2 A switch Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170Fig. 6.28: Using a switch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

6.4 Search Loop Application: Guess My Number . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

Page 9: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS ix

Fig. 6.29: Problem specification: Guess my number. . . . . . . . . . . . . . . . . . 172Fig. 6.30: An input-driven search loop. . . . . . . . . . . . . . . . . . . . . . . . . . 172Fig. 6.31: A counted sentinel loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

6.5 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1756.5.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1756.5.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1756.5.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1766.5.4 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1776.5.5 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

6.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1776.6.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1776.6.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1796.6.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

7 Using Numeric Types 1857.1 Number Systems and Number Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

Fig. 7.1: Place values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1857.2 Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

Fig. 7.2: Names for integer types. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1857.2.1 Signed and Unsigned Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

Fig. 7.3: Two’s complement representation of integers. . . . . . . . . . . . . . . . 1867.2.2 Short and long integers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

Fig. 7.4: ISO C integer representations. . . . . . . . . . . . . . . . . . . . . . . . . 187Fig. 7.5: Integer literals in base 10. . . . . . . . . . . . . . . . . . . . . . . . . . . 188

7.3 Floating-Point Types in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1897.3.1 Representation of Real Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

Fig. 7.6: Binary representation of reals. . . . . . . . . . . . . . . . . . . . . . . . . 189Fig. 7.7: IEEE floating-point types. . . . . . . . . . . . . . . . . . . . . . . . . . . 190Fig. 7.8: Floating-point literal examples . . . . . . . . . . . . . . . . . . . . . . . 191

7.4 Reading and Writing Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1917.4.1 Integer Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

Fig. 7.9: Integer conversion specifications. . . . . . . . . . . . . . . . . . . . . . . 1917.4.2 Integer Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

Fig. 7.10: Incorrect conversions produce garbabe output. . . . . . . . . . . . . . . . 1927.4.3 Floating-Point Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1937.4.4 Floating-Point Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

Fig. 7.11: Basic floating-point conversion specifications. . . . . . . . . . . . . . . . 193Fig. 7.12: The %f output conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . 193Fig. 7.13: The %e output conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . 193Fig. 7.14: Sometimes %g output looks like an integer. . . . . . . . . . . . . . . . . . 193Fig. 7.15: Sometimes %g looks like %e. . . . . . . . . . . . . . . . . . . . . . . . . . 193Fig. 7.16: For tiny numbers, %g looks like %e. . . . . . . . . . . . . . . . . . . . . . 193Fig. 7.17: Sometimes %g looks like %f. . . . . . . . . . . . . . . . . . . . . . . . . . 193Fig. 7.18: Output conversion specifiers. . . . . . . . . . . . . . . . . . . . . . . . . . 195

7.4.5 One Number may Appear in Many Ways . . . . . . . . . . . . . . . . . . . . . . . . . 1957.5 Mixing Types in Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

7.5.1 Basic Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Fig. 7.19: Converting a float to a double. . . . . . . . . . . . . . . . . . . . . . . 197

7.5.2 Type Casts and Coercions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Fig. 7.20: Kinds of casts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Fig. 7.21: Rounding and truncation. . . . . . . . . . . . . . . . . . . . . . . . . . . 198Fig. 7.22: Type coercion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199

Page 10: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

x CONTENTS

7.5.3 Diagramming Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200Fig. 7.23: Diagramming a cast and a coercion. . . . . . . . . . . . . . . . . . . . . 200Fig. 7.24: Expressions with casts and coercions. . . . . . . . . . . . . . . . . . . . . 200Fig. 7.25: Problem specification: computing resistance. . . . . . . . . . . . . . . . . 201

7.5.4 Using Type Casts to Avoid Integer Division Problems . . . . . . . . . . . . . . . . . . 202Fig. 7.26: Computing resistance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

7.6 The Trouble with Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2037.6.1 Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204

Fig. 7.27: Overflow and wrap with a four bit signed integer. . . . . . . . . . . . . . 204Fig. 7.28: Computing N!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

7.6.2 Underflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207Fig. 7.29: Floating-point underflow. . . . . . . . . . . . . . . . . . . . . . . . . . . 207

7.6.3 Orders of Magnitude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2077.6.4 Not a Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208

Fig. 7.30: Calculation order matters. . . . . . . . . . . . . . . . . . . . . . . . . . . 2097.6.5 Representational Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2097.6.6 Making Meaningful Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

Fig. 7.31: An approximate comparison. . . . . . . . . . . . . . . . . . . . . . . . . . 210Fig. 7.32: Comparing floats for equality. . . . . . . . . . . . . . . . . . . . . . . . . 210

7.6.7 Application: Cruise Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211Fig. 7.33: Problem specification: Cruise control. . . . . . . . . . . . . . . . . . . . . 211Fig. 7.34: Cruise control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

7.7 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2127.7.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2127.7.2 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

Fig. 7.35: Casts and conversions in C. . . . . . . . . . . . . . . . . . . . . . . . . . 2157.7.3 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2167.7.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2177.7.5 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

7.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2197.8.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2197.8.2 Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2217.8.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

8 Character Data 2278.1 Representation of Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

Fig. 8.1: The case bit in ASCII. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2278.1.1 Character Types in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2288.1.2 The Different Interpretations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

Fig. 8.2: Character types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2288.1.3 Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

Fig. 8.3: Writing character constants. . . . . . . . . . . . . . . . . . . . . . . . . . 228Fig. 8.4: Useful predefined escape sequences. . . . . . . . . . . . . . . . . . . . . . 228

8.2 Input and Output with Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2298.2.1 Character Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2298.2.2 Character Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

Fig. 8.5: Printing the ASCII codes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 2308.2.3 Using the I/O Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

Fig. 8.6: Character input and output. . . . . . . . . . . . . . . . . . . . . . . . . . 2318.2.4 Other ways to skip whitespace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

Fig. 8.7: A function for skipping whitespace. . . . . . . . . . . . . . . . . . . . . . 2338.3 Operations on Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

Page 11: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS xi

8.3.1 Characters Are Very Short Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234Fig. 8.8: Character operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

8.3.2 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2348.3.3 Comparing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2358.3.4 Character Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2358.3.5 Other Character Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

8.4 Character Application: An Improved Processing Loop . . . . . . . . . . . . . . . . . . . . . . 236Fig. 8.9: Improving the workmaster. . . . . . . . . . . . . . . . . . . . . . . . . . . 236Fig. 8.10: Using characters in a switch. . . . . . . . . . . . . . . . . . . . . . . . . . 236

8.5 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2398.5.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2398.5.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2408.5.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2408.5.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240

8.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2408.6.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2408.6.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2418.6.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

9 Program Design 2479.1 Modular Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2479.2 Communication Between Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

9.2.1 The Function Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248Fig. 9.1: Information flow in pow2(). . . . . . . . . . . . . . . . . . . . . . . . . . 248

9.2.2 Parameter Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249Fig. 9.2: The run-time stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

9.3 Parameter Type Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250Fig. 9.3: The declared prototype controls all. . . . . . . . . . . . . . . . . . . . . . 250Fig. 9.4: The importance of parameter order. . . . . . . . . . . . . . . . . . . . . 251

9.4 Data Modularity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252Fig. 9.5: Gas models before global elimination. . . . . . . . . . . . . . . . . . . . . 253Fig. 9.6: A call graph for the CO gas program. . . . . . . . . . . . . . . . . . . . . 253Fig. 9.7: Functions for gas models before global elimination. . . . . . . . . . . . . 253

9.4.1 Scope and Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2539.4.2 Global and Local Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

Fig. 9.8: Name scoping in gas models program, before global elimination. . . . . . 2559.4.3 Eliminating Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257

Fig. 9.9: Eliminating the global variable. . . . . . . . . . . . . . . . . . . . . . . . 257Fig. 9.10: Functions for gas models after global elimination. . . . . . . . . . . . . . 257

9.5 Program Design and Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2599.5.1 The Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

Fig. 9.11: Problem specification and test plan: fin temperature. . . . . . . . . . . . 2599.5.2 Applying the Process: Stepwise Development of a Program . . . . . . . . . . . . . . . 259

Fig. 9.12: First draft of main() for the fin program, with a function stub. . . . . . 261Fig. 9.13: Fin program: First draft of print_table() function. . . . . . . . . . . . 263Fig. 9.14: Temperature along a cooling fin. . . . . . . . . . . . . . . . . . . . . . . 265

9.6 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2679.6.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2679.6.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2679.6.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2689.6.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

9.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

Page 12: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

xii CONTENTS

9.7.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2699.7.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2709.7.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

10 An Introduction to Arrays 27710.1 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

10.1.1 Array Declarations and Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278Fig. 10.1: Declaring an array of five floats. . . . . . . . . . . . . . . . . . . . . . . . 278Fig. 10.2: An initialized array of short ints. . . . . . . . . . . . . . . . . . . . . . . 278Fig. 10.3: Length and initializer options. . . . . . . . . . . . . . . . . . . . . . . . . 278Fig. 10.4: Initializing character arrays. . . . . . . . . . . . . . . . . . . . . . . . . . 279

10.1.2 The Size of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279Fig. 10.5: The size of an array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

10.1.3 Accessing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280Fig. 10.6: Simple subscripts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281Fig. 10.7: Computed subscripts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281Fig. 10.8: Subscript demo, the magnitude of a vector in 3-space. . . . . . . . . . . 282

10.1.4 Subscript Out-of-Range Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28310.2 Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

10.2.1 Array Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283Fig. 10.9: Filling an array with data. . . . . . . . . . . . . . . . . . . . . . . . . . . 283

10.2.2 Walking on Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284Fig. 10.10: Walking on memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284Fig. 10.11: Before and after walking on memory. . . . . . . . . . . . . . . . . . . . . 284

10.3 Parallel Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286Fig. 10.12: Problem specifications: Exam averages. . . . . . . . . . . . . . . . . . . . 286Fig. 10.13: Using parallel arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286Fig. 10.14: Parallel arrays can represent a table. . . . . . . . . . . . . . . . . . . . . 286

10.4 Array Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28910.5 An Array Application: Prime Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

Fig. 10.15: Problem specifications: A table of prime numbers. . . . . . . . . . . . . 290Fig. 10.16: Calculating prime numbers. . . . . . . . . . . . . . . . . . . . . . . . . . 290Fig. 10.17: Functions for the prime number program. . . . . . . . . . . . . . . . . . 292

10.6 Searching an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294Fig. 10.18: Problem specifications: Recording bill payments. . . . . . . . . . . . . . 294Fig. 10.19: Main program for sequential search. . . . . . . . . . . . . . . . . . . . . . 294Fig. 10.20: Input and output functions for parallel arrays. . . . . . . . . . . . . . . . 297Fig. 10.21: Sequential search of a table. . . . . . . . . . . . . . . . . . . . . . . . . . 298Fig. 10.22: Searching without a second return statement. . . . . . . . . . . . . . . . 298

10.7 The Maximum Value in an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300Fig. 10.23: Who owes the most? Main program for finding the maximum. . . . . . . 300Fig. 10.24: Finding the maximum value. . . . . . . . . . . . . . . . . . . . . . . . . . 300Fig. 10.25: The maximum algorithm, step by step. . . . . . . . . . . . . . . . . . . . 300

10.8 Sorting by Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301Fig. 10.26: The selection sort algorithm, step by step. . . . . . . . . . . . . . . . . . 301Fig. 10.27: Problem specifications: Sorting the Billing Records . . . . . . . . . . . . 303

10.8.1 The Main Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303Fig. 10.28: Call chart for selection sort. . . . . . . . . . . . . . . . . . . . . . . . . . 304

10.8.2 Developing the sort_data() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 304Fig. 10.29: Main program for selection sort. . . . . . . . . . . . . . . . . . . . . . . . 304Fig. 10.30: Sorting by selecting the maximum. . . . . . . . . . . . . . . . . . . . . . 305Fig. 10.31: Input and output for selection sort. . . . . . . . . . . . . . . . . . . . . . 305

Page 13: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS xiii

10.9 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30610.9.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30610.9.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30710.9.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30810.9.4 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30910.9.5 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

10.10Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31010.10.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31010.10.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31210.10.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313

11 An Introduction to Pointers 31711.1 A First Look at Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

11.1.1 Pointer Values Are Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317Fig. 11.1: A pointer and its referent. . . . . . . . . . . . . . . . . . . . . . . . . . . 317Fig. 11.2: Pointer diagrams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317Fig. 11.3: Declaring a pointer variable. . . . . . . . . . . . . . . . . . . . . . . . . . 317Fig. 11.4: Initializing a pointer variable. . . . . . . . . . . . . . . . . . . . . . . . . 318

11.1.2 Pointer Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319Fig. 11.5: Pointers in memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319Fig. 11.6: Pointer operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320

11.2 Call by Value/Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32111.2.1 Address Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32211.2.2 Pointer Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322

Fig. 11.7: Call by value/address. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322Fig. 11.8: A swap function with an error. . . . . . . . . . . . . . . . . . . . . . . . 325Fig. 11.9: Seeing the difference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325Fig. 11.10: A swap function that works. . . . . . . . . . . . . . . . . . . . . . . . . . 325

11.2.3 A More Complex Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32511.3 Application: Statistical Measures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326

Fig. 11.11: Problem specifications: Statistics. . . . . . . . . . . . . . . . . . . . . . . 326Fig. 11.12: Mean and standard deviation. . . . . . . . . . . . . . . . . . . . . . . . . 326Fig. 11.13: Call graph for the mean and standard deviation program. . . . . . . . . 326Fig. 11.14: The get_data() and stats() functions. . . . . . . . . . . . . . . . . . . 326

11.3.1 Summary: Returning Results from a Function . . . . . . . . . . . . . . . . . . . . . . 33011.4 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330

11.4.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33011.4.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33111.4.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33111.4.4 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33211.4.5 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332

11.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33211.5.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33211.5.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33411.5.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

12 Strings 34112.1 String Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341

12.1.1 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341Fig. 12.1: String literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342Fig. 12.2: Quotes and comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342

12.1.2 A String Is a Pointer to an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342

Page 14: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

xiv CONTENTS

Fig. 12.3: Three kinds of nothing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343Fig. 12.4: Implementation of a string variable. . . . . . . . . . . . . . . . . . . . . . 343Fig. 12.5: Selecting the appropriate answer. . . . . . . . . . . . . . . . . . . . . . . 343

12.1.3 Declare an Array to Get a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344Fig. 12.6: An array of characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344

12.1.4 Array vs. String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344Fig. 12.7: Array vs. string. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345Fig. 12.8: A character array and a string. . . . . . . . . . . . . . . . . . . . . . . . 345

12.2 String I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345Fig. 12.9: String literals and string output. . . . . . . . . . . . . . . . . . . . . . . 345

12.2.1 String Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34512.2.2 String Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348

Fig. 12.10: You cannot store an input string in a pointer. . . . . . . . . . . . . . . . 348Fig. 12.11: You need an array to store an input string. . . . . . . . . . . . . . . . . 348

12.3 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35012.3.1 Strings as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350

Fig. 12.12: A string parameter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35012.3.2 The String Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

Fig. 12.13: The size of a string. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351Fig. 12.14: Computing the length of a string. . . . . . . . . . . . . . . . . . . . . . . 352Fig. 12.15: Do not use == with strings. . . . . . . . . . . . . . . . . . . . . . . . . . 352Fig. 12.16: How to compare strings for alphabetic order. . . . . . . . . . . . . . . . 353Fig. 12.17: Possible implementation of strcmp(). . . . . . . . . . . . . . . . . . . . 353Fig. 12.18: Searching for a character or a substring. . . . . . . . . . . . . . . . . . . 354Fig. 12.19: Copy and concatenate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354

12.4 Arrays of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355Fig. 12.20: A ragged array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

12.4.1 The Menu Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35512.4.2 An Example: Selling Ice Cream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356

Fig. 12.21: Selecting ice cream from a menu. . . . . . . . . . . . . . . . . . . . . . . 356Fig. 12.22: A menu-handling function. . . . . . . . . . . . . . . . . . . . . . . . . . . 357

12.4.3 Subscript Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35912.4.4 The menu_c() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

Fig. 12.23: The menu_c() function. . . . . . . . . . . . . . . . . . . . . . . . . . . . 36012.5 String Processing Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361

12.5.1 Menu Processing and String Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361Fig. 12.24: Magic memo maker. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361Fig. 12.25: Using a string variable with a menu. . . . . . . . . . . . . . . . . . . . . 361Fig. 12.26: Flow diagram with a menu loop. . . . . . . . . . . . . . . . . . . . . . . 361Fig. 12.27: Composing and printing the memo. . . . . . . . . . . . . . . . . . . . . . 361

12.5.2 Password Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365Fig. 12.28: Password validation: Comparing and copying strings. . . . . . . . . . . . 366

12.5.3 Table-driven Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367Fig. 12.29: The illnesses and the class. . . . . . . . . . . . . . . . . . . . . . . . . . . 367Fig. 12.30: Preparing the form letter. . . . . . . . . . . . . . . . . . . . . . . . . . . 368Fig. 12.31: Composing the letters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

12.6 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37012.6.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37012.6.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37112.6.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37112.6.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37212.6.5 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

Page 15: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS xv

12.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37312.7.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37312.7.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37512.7.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377

13 Enumerated and Structured Types 38113.1 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

13.1.1 Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381Fig. 13.1: The form of an enum declaration. . . . . . . . . . . . . . . . . . . . . . . 382Fig. 13.2: Four enumerations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382Fig. 13.3: Using an enumeration to name errors. . . . . . . . . . . . . . . . . . . . 383

13.1.2 Printing Enumeration Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38313.1.3 Returning Multiple Function Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384

Fig. 13.4: Returning three results. . . . . . . . . . . . . . . . . . . . . . . . . . . . 384Fig. 13.5: The quadratic root function. . . . . . . . . . . . . . . . . . . . . . . . . . 384Fig. 13.6: Memory for the quadratic root program. . . . . . . . . . . . . . . . . . . 384

13.2 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387Fig. 13.7: Modern syntax for a struct declaration. . . . . . . . . . . . . . . . . . 387Fig. 13.8: A struct type declaration. . . . . . . . . . . . . . . . . . . . . . . . . . 388Fig. 13.9: Declaring and initializing a structure. . . . . . . . . . . . . . . . . . . . . 389

13.3 Operations on Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38913.3.1 Structure Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390

Fig. 13.10: Set and use a pointer to a structure. . . . . . . . . . . . . . . . . . . . . 390Fig. 13.11: Access one member of a structure. . . . . . . . . . . . . . . . . . . . . . 390Fig. 13.12: Structure assignment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390

13.3.2 Using Structures with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391Fig. 13.13: Returning a structure from a function. . . . . . . . . . . . . . . . . . . 391Fig. 13.14: Call by value with a structure. . . . . . . . . . . . . . . . . . . . . . . . . 391Fig. 13.15: Call by value/address with a structure. . . . . . . . . . . . . . . . . . . . 392Fig. 13.16: An array of structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394

13.3.3 Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39413.3.4 Comparing Two Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

Fig. 13.17: Comparing two structures. . . . . . . . . . . . . . . . . . . . . . . . . . . 395Fig. 13.18: Declarations for the lumber program. . . . . . . . . . . . . . . . . . . . . 396Fig. 13.19: Structure operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396

13.3.5 Putting the Pieces Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39613.4 Application: Points in a Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398

Fig. 13.20: A rectangle on the xy-plane. . . . . . . . . . . . . . . . . . . . . . . . . 398Fig. 13.21: Problem specifications: Points in a rectangle. . . . . . . . . . . . . . . . 398Fig. 13.22: The test plan for points in a rectangle. . . . . . . . . . . . . . . . . . . . 400Fig. 13.23: Main program for points in a rectangle. . . . . . . . . . . . . . . . . . . 400Fig. 13.24: Two structured types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400Fig. 13.25: Testing points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402Fig. 13.26: Reading a rectangle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402Fig. 13.27: Comparing structured data. . . . . . . . . . . . . . . . . . . . . . . . . . 404Fig. 13.28: Point location. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405

13.5 Application: The Monte Carlo Method (Optional Topic) . . . . . . . . . . . . . . . . . . . . . 408Fig. 13.29: Specifications: Monte Carlo approximation of an area. . . . . . . . . . . 408

13.5.1 Problem Description, Specifications, and Testing . . . . . . . . . . . . . . . . . . . . . 40813.5.2 Developing the Main Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409

Fig. 13.30: A Monte Carlo approximation. . . . . . . . . . . . . . . . . . . . . . . . 40913.5.3 Developing the Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

Page 16: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

xvi CONTENTS

Fig. 13.31: Flow diagram for the Monte Carlo loop. . . . . . . . . . . . . . . . . . . 411Fig. 13.32: Functions for the Monte Carlo simulation. . . . . . . . . . . . . . . . . 411

13.5.4 Testing, Output, and Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41213.6 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412

13.6.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41213.6.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41313.6.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41413.6.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41413.6.5 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415

13.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41513.7.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41513.7.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41713.7.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419

14 Streams and Files 42514.1 Streams and Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425

14.1.1 Stream I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425Fig. 14.1: A stream carries data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426Fig. 14.2: The three default streams. . . . . . . . . . . . . . . . . . . . . . . . . . . 426Fig. 14.3: Stream redirection in UNIX. . . . . . . . . . . . . . . . . . . . . . . . . . 427Fig. 14.4: Redirecting stdout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427

14.1.2 Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427Fig. 14.5: A buffer is a window on the data. . . . . . . . . . . . . . . . . . . . . . . 428

14.2 Programmer-Defined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42814.2.1 Defining and Using Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

Fig. 14.6: Streams and fopen(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429Fig. 14.7: Programmer-defined streams. . . . . . . . . . . . . . . . . . . . . . . . . 429

14.2.2 File Management Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43014.3 Stream Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431

14.3.1 Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431Fig. 14.8: Specifications: Writing a date file from an experiment. . . . . . . . . . . 432

14.3.2 Writing Data to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432Fig. 14.9: Writing a file of voltages. . . . . . . . . . . . . . . . . . . . . . . . . . . . 433

14.4 Stream Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43514.4.1 Input Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43514.4.2 Detecting End of File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43614.4.3 Reading Data from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

Fig. 14.10: Reading a data file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437Fig. 14.11: Handling a fatal error. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438

14.5 Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43914.5.1 Format Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439

Fig. 14.12: Correct and mismatched formats. . . . . . . . . . . . . . . . . . . . . . . 44014.5.2 File Processing Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44114.5.3 Error Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442

Fig. 14.13: The cleanline() function. . . . . . . . . . . . . . . . . . . . . . . . . . 442Fig. 14.14: The clean_and_log() function. . . . . . . . . . . . . . . . . . . . . . . . 442

14.5.4 Using the Return Value of scanf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443Fig. 14.15: Testing the code returned by scanf(). . . . . . . . . . . . . . . . . . . . 443Fig. 14.16: Error reading second value. . . . . . . . . . . . . . . . . . . . . . . . . . 443Fig. 14.17: Error reading third value. . . . . . . . . . . . . . . . . . . . . . . . . . . 443

14.5.5 A Combined Technique for Error Recovery . . . . . . . . . . . . . . . . . . . . . . . . 445Fig. 14.18: Handling end-of-file and file input errors. . . . . . . . . . . . . . . . . . . 445

Page 17: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS xvii

Fig. 14.19: Error report for recovery. . . . . . . . . . . . . . . . . . . . . . . . . . . . 44714.6 Reading and Writing Raw Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448

Fig. 14.20: Reading and writing raw data. . . . . . . . . . . . . . . . . . . . . . . . . 449Fig. 14.21: Setting up the streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449Fig. 14.22: Copying the file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449

14.7 File Application: Random Selection Without Replacement . . . . . . . . . . . . . . . . . . . . 451Fig. 14.23: Problem specifications: A quiz. . . . . . . . . . . . . . . . . . . . . . . . 452Fig. 14.24: An elemental quiz. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452Fig. 14.25: The get_quiz() function: Read the quiz data from a file. . . . . . . . . 452Fig. 14.26: The do_quiz() function: Ask quiz questions in random order. . . . . . . 455Fig. 14.27: The array after two questions. . . . . . . . . . . . . . . . . . . . . . . . . 456Fig. 14.28: The array after four questions. . . . . . . . . . . . . . . . . . . . . . . . . 456Fig. 14.29: The array after six questions. . . . . . . . . . . . . . . . . . . . . . . . . 456Fig. 14.30: The ask() function: A question and an answer. . . . . . . . . . . . . . . 456

14.8 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45914.8.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45914.8.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46014.8.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46014.8.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46114.8.5 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461

14.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46214.9.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46214.9.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46314.9.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

Fig. 14.31: Program for Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

15 Calculating with Bits 46915.1 Hexadecimal Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469

Fig. 15.1: Hexadecimal numeric literals. . . . . . . . . . . . . . . . . . . . . . . . . 47015.1.1 Hexadecimal I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

Fig. 15.2: Hexadecimal character literals. . . . . . . . . . . . . . . . . . . . . . . . 470Fig. 15.3: Conversion specifiers for unsigned values. . . . . . . . . . . . . . . . . . . 470Fig. 15.4: Formatting unsigned integers. . . . . . . . . . . . . . . . . . . . . . . . . 471

15.2 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472Fig. 15.5: Bitwise operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472

15.2.1 Masks and Masking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472Fig. 15.6: Bit masks for encrypting a number. . . . . . . . . . . . . . . . . . . . . . 472Fig. 15.7: Truth tables for bitwise operators. . . . . . . . . . . . . . . . . . . . . . 473Fig. 15.8: Bitwise AND (&). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473Fig. 15.9: Bitwise OR (|). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473Fig. 15.10: Bitwise XOR (^). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473Fig. 15.11: Just say no. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474

15.2.2 Shift Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475Fig. 15.12: Left and right shifts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475

15.2.3 Example: Shifting and Masking an Internet Address . . . . . . . . . . . . . . . . . . . 476Fig. 15.13: Problem specifications: Decoding an Internet address. . . . . . . . . . . 476Fig. 15.14: Decoding an Internet address. . . . . . . . . . . . . . . . . . . . . . . . . 476Fig. 15.15: Problem specifications: Encrypting and decrypting a number. . . . . . . 477

15.3 Application: Simple Encryption and Decryption . . . . . . . . . . . . . . . . . . . . . . . . . 478Fig. 15.16: Encrypting a number. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478Fig. 15.17: Decrypting a number. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478

15.4 Bitfield Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480

Page 18: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

xviii CONTENTS

Fig. 15.18: Bitfield-structure demonstration. . . . . . . . . . . . . . . . . . . . . . . 48115.4.1 Bitfield Application: A Device Controller (Advanced Topic) . . . . . . . . . . . . . . . 483

Fig. 15.19: Problem specifications: Skylight controller. . . . . . . . . . . . . . . . . . 483Fig. 15.20: Declarations for the skylight controller: skylight.h. . . . . . . . . . . . 484Fig. 15.21: A skylight controller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485Fig. 15.22: Operations for the skylight controller. . . . . . . . . . . . . . . . . . . . 486

15.5 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48915.5.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489

Fig. 15.23: Summary of bitwise operators. . . . . . . . . . . . . . . . . . . . . . . . . 48915.5.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49015.5.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49015.5.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491

15.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49115.6.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49115.6.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49215.6.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493

Fig. 15.24: Problem 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497

16 Dynamic Arrays 50116.1 Dynamic Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501

Fig. 16.1: Dynamic memory allocation functions. . . . . . . . . . . . . . . . . . . 50116.1.1 Mass Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50116.1.2 Cleared Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50316.1.3 Freeing Dynamic Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50416.1.4 Resizing an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504

Fig. 16.2: Sorting using a dynamic array. . . . . . . . . . . . . . . . . . . . . . . . 50516.1.5 Using Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505

Fig. 16.3: A variable number of varying-sized objects. . . . . . . . . . . . . . . . . 50716.1.6 Arrays of Dynamic Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507

Fig. 16.4: Building a ragged array. . . . . . . . . . . . . . . . . . . . . . . . . . . . 509Fig. 16.5: A dynamic ragged array. . . . . . . . . . . . . . . . . . . . . . . . . . . . 510

16.2 Using Dynamic Arrays: A Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51016.2.1 Transient Heat Conduction in a Semi-Infinite Slab . . . . . . . . . . . . . . . . . . . . 511

Fig. 16.6: Heat conduction in a semi-infinite slab. . . . . . . . . . . . . . . . . . . . 51116.2.2 Simulating the Cooling Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512

Fig. 16.7: A call chart for the heat flow simulation. . . . . . . . . . . . . . . . . . . 512Fig. 16.8: A heat flow simulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512Fig. 16.9: Parameters for the simulation. . . . . . . . . . . . . . . . . . . . . . . . . 512Fig. 16.10: Iteration for the heat flow simulation, sim_cool(). . . . . . . . . . . . . 515Fig. 16.11: Print function for the heat flow simulation. . . . . . . . . . . . . . . . . . 517

16.3 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51816.3.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51816.3.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51916.3.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51916.3.4 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52016.3.5 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520

16.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52016.4.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52016.4.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52116.4.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

Page 19: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS xix

17 Working with Pointers 52717.1 Pointers—Old and New Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527

17.1.1 Pointer Declarations and Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . 527Fig. 17.1: Pointing at a variable or a structure. . . . . . . . . . . . . . . . . . . . . 528Fig. 17.2: Pointing at an array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528Fig. 17.3: Declaring a function pointer. . . . . . . . . . . . . . . . . . . . . . . . . . 528Fig. 17.4: Using function pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . 529

17.1.2 Using Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529Fig. 17.5: Indirect reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530Fig. 17.6: Direct and indirect assignment. . . . . . . . . . . . . . . . . . . . . . . . 530Fig. 17.7: Input and output through pointers. . . . . . . . . . . . . . . . . . . . . . 531

17.1.3 Pointer Arithmetic and Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531Fig. 17.8: Pointer addition and subtraction. . . . . . . . . . . . . . . . . . . . . . . 531Fig. 17.9: Incrementing a pointer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532Fig. 17.10: Pointer comparisons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532

17.2 Application: A Menu of Pointers to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 533Fig. 17.11: Selecting a function from a menu. . . . . . . . . . . . . . . . . . . . . . . 533Fig. 17.12: An improved menu function. . . . . . . . . . . . . . . . . . . . . . . . . . 535

17.3 Using Pointers with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53617.3.1 Scanners and Sentinels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536

Fig. 17.13: An array with a cursor and a sentinel. . . . . . . . . . . . . . . . . . . . 536Fig. 17.14: An increment loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536Fig. 17.15: An on-board sentinel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537

17.3.2 Packaging and Array with its Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53817.3.3 Input and Output with a Data Pack . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538

Fig. 17.16: Input into an array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538Fig. 17.17: Output from an array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539

17.4 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540Fig. 17.18: The main() program for insertion sort. . . . . . . . . . . . . . . . . . . . 541Fig. 17.19: Setup for insertion sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . 541

17.4.1 Sorting by insertion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541Fig. 17.20: Insertion sort for ascending order. . . . . . . . . . . . . . . . . . . . . . . 543Fig. 17.21: Insertion sort step by step. . . . . . . . . . . . . . . . . . . . . . . . . . . 543Fig. 17.22: Search and move. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543

17.5 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54617.5.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54617.5.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54717.5.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54817.5.4 Where to Find More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54917.5.5 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549

17.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54917.6.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54917.6.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55117.6.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552

Fig. 17.23: Poker hands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555Fig. 17.24: Roulette bets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555

Page 20: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

xx CONTENTS

18 Array Data Structures 55718.1 Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557

18.1.1 Declarations and Memory Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557Fig. 18.1: A two-dimensional array. . . . . . . . . . . . . . . . . . . . . . . . . . . . 557Fig. 18.2: Layout of an array in memory. . . . . . . . . . . . . . . . . . . . . . . . . 557

18.1.2 Using typedef for Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . 558Fig. 18.3: Using typedef for 2D arrays. . . . . . . . . . . . . . . . . . . . . . . . . 558Fig. 18.4: Travel time matrix. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558

18.1.3 A Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559Fig. 18.5: Travel time for a two-day trip. . . . . . . . . . . . . . . . . . . . . . . . . 559

18.1.4 An Array of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560Fig. 18.6: Declaring an array of arrays. . . . . . . . . . . . . . . . . . . . . . . . . . 561Fig. 18.7: Calculating wind speed. . . . . . . . . . . . . . . . . . . . . . . . . . . . 561Fig. 18.8: Printing the wind speed table. . . . . . . . . . . . . . . . . . . . . . . . . 561

18.1.5 Dynamic Matrix: An Array of Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . 56418.1.6 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564

Fig. 18.9: A three-dimensional array. . . . . . . . . . . . . . . . . . . . . . . . . . . 564Fig. 18.10: Problem specifications: 2D point transformation. . . . . . . . . . . . . . 565

18.2 Application: Transformation of 2D Point Coordinates . . . . . . . . . . . . . . . . . . . . . . 565Fig. 18.11: 2D point transformation—main program. . . . . . . . . . . . . . . . . . 565Fig. 18.12: Reading and displaying the data. . . . . . . . . . . . . . . . . . . . . . . 568Fig. 18.13: The transformation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568

18.3 Application: Image Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57118.3.1 Smoothing eliminates extreme pixel values. . . . . . . . . . . . . . . . . . . . . . . . . 571

Fig. 18.14: Problem specifications: Image smoothing, main program. . . . . . . . . . 57118.3.2 The .pgm image format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572

Fig. 18.15: The P5 file format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572Fig. 18.16: Image smoothing—main program. . . . . . . . . . . . . . . . . . . . . . . 572Fig. 18.17: Reading program control data. . . . . . . . . . . . . . . . . . . . . . . . 575Fig. 18.18: Reading an image file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576Fig. 18.19: Skipping whitespace and comments. . . . . . . . . . . . . . . . . . . . . 576Fig. 18.20: Preparing the new image structure. . . . . . . . . . . . . . . . . . . . . . 576Fig. 18.21: The smoothing calculation. . . . . . . . . . . . . . . . . . . . . . . . . . 576Fig. 18.22: Writing the .pgm image file. . . . . . . . . . . . . . . . . . . . . . . . . . 576Fig. 18.23: Results of image smoothing program. . . . . . . . . . . . . . . . . . . . . 580

18.4 Application: Gaussian Elimination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58018.4.1 An Implementation of Gauss’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 582

Fig. 18.24: A call chart for Gaussian elimination. . . . . . . . . . . . . . . . . . . . . 582Fig. 18.25: Solving a system of linear equations. . . . . . . . . . . . . . . . . . . . . 582Fig. 18.26: Reading the equations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584Fig. 18.27: Print the equations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584Fig. 18.28: Gaussian elimination. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587Fig. 18.29: Pivoting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587Fig. 18.30: Scaling the kth equation. . . . . . . . . . . . . . . . . . . . . . . . . . . . 588Fig. 18.31: Using the pivot equation to clear coefficients. . . . . . . . . . . . . . . . 590Fig. 18.32: Printing the answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590Fig. 18.33: A system of equations with no solution. . . . . . . . . . . . . . . . . . . 591

18.5 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59118.5.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59118.5.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59318.5.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59318.5.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594

Page 21: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS xxi

18.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59418.6.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59418.6.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59518.6.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596

Fig. 18.34: Effective temperature decrease due to wind chill. . . . . . . . . . . . . . 601

19 Recursion 60519.1 Storage Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605

19.1.1 Automatic Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60519.1.2 Static Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60619.1.3 External Storage (Advanced Topic) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606

19.2 The Run-Time Stack (Advanced Topic) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60719.2.1 Stack Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60719.2.2 Stack Diagrams and Program Traces . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608

Fig. 19.1: The run-time stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608Fig. 19.2: The run-time stack for the gas pressure program. . . . . . . . . . . . . 608

19.3 Iteration and Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60919.3.1 The Nature of Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60919.3.2 The Nature of Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610

Fig. 19.3: A recursive program to find a minimum. . . . . . . . . . . . . . . . . . . 61019.3.3 Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610

19.4 A Simple Example of Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61119.5 A More Complex Example: Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613

Fig. 19.4: Call chart for binary search. . . . . . . . . . . . . . . . . . . . . . . . . . 614Fig. 19.5: The binary search program. . . . . . . . . . . . . . . . . . . . . . . . . . 614Fig. 19.6: Setting up the data structures. . . . . . . . . . . . . . . . . . . . . . . . 614Fig. 19.7: Getting and checking the data. . . . . . . . . . . . . . . . . . . . . . . . 614Fig. 19.8: The binary search function, bin_search(). . . . . . . . . . . . . . . . . 618Fig. 19.9: Beginning a binary search. . . . . . . . . . . . . . . . . . . . . . . . . . . 618Fig. 19.10: Second active call. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618Fig. 19.11: Third active call. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619Fig. 19.12: Fourth and last active call. . . . . . . . . . . . . . . . . . . . . . . . . . . 619Fig. 19.13: Call graph for the quicksort program. . . . . . . . . . . . . . . . . . . . . 620

19.6 Quicksort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621Fig. 19.14: Calling the quicksort() function. . . . . . . . . . . . . . . . . . . . . . 621Fig. 19.15: Getting ready to sort: quicksort(). . . . . . . . . . . . . . . . . . . . . 621Fig. 19.16: Ready to sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621Fig. 19.17: The quick() function performs the recursion. . . . . . . . . . . . . . . . 623Fig. 19.18: Positions at the end of the first pass. . . . . . . . . . . . . . . . . . . . . 623Fig. 19.19: The recursive calls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623

19.6.1 The Partition Step: Divide to Conquer . . . . . . . . . . . . . . . . . . . . . . . . . . . 625Fig. 19.20: Starting the sort. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625Fig. 19.21: Putting the pivot at the beginning. . . . . . . . . . . . . . . . . . . . . . 625Fig. 19.22: Starting the scan. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626Fig. 19.23: Continuing the scan. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626Fig. 19.24: The scanners meet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626Fig. 19.25: Finishing the pass. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626Fig. 19.26: The partition() function. . . . . . . . . . . . . . . . . . . . . . . . . . 626

19.6.2 Possible Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62819.7 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629

19.7.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62919.7.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630

Page 22: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

xxii CONTENTS

19.7.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63019.7.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631

19.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63119.8.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63119.8.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63219.8.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633

20 Making Programs General 63720.1 Functions with Variable Numbers of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 637

Fig. 20.1: A unified fatal() function. . . . . . . . . . . . . . . . . . . . . . . . . . 63720.2 Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638

20.2.1 The Argument Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639Fig. 20.2: The argument vector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639

20.2.2 Decoding Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639Fig. 20.3: Using command-line arguments. . . . . . . . . . . . . . . . . . . . . . . . 639Fig. 20.4: Decoding command-line arguments. . . . . . . . . . . . . . . . . . . . . . 641Fig. 20.5: A revised sort function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644

20.3 Void* and Functions as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64520.3.1 Using the System’s qsort() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 645

Fig. 20.6: Two comparison functions for qsort(). . . . . . . . . . . . . . . . . . . . 646Fig. 20.7: Using qsort() and a functional parameter. . . . . . . . . . . . . . . . . 646

20.3.2 Using the System’s bsearch() Function . . . . . . . . . . . . . . . . . . . . . . . . . . 648Fig. 20.8: Calling on bsearch(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648Fig. 20.9: A comparison function for integers. . . . . . . . . . . . . . . . . . . . . . 650

20.4 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65020.4.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65020.4.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65120.4.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65220.4.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652

20.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65220.5.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65220.5.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65320.5.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654

21 Modular Organization 65721.1 File Management Issues with Modular Construction . . . . . . . . . . . . . . . . . . . . . . . 65721.2 Building a Multimodule Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659

21.2.1 Using an IDE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66021.2.2 Using a makefile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660

Fig. 21.1: Steps in building a program. . . . . . . . . . . . . . . . . . . . . . . . . . 66121.2.3 A UNIX makefile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662

Fig. 21.2: A UNIX makefile for game. . . . . . . . . . . . . . . . . . . . . . . . . . . 66321.3 What You Should Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665

21.3.1 Major Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66521.3.2 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66521.3.3 Sticky Points and Common Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66521.3.4 New and Revisited Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666

21.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66621.4.1 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66621.4.2 Using Pencil and Paper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66621.4.3 Using the Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667

Page 23: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS xxiii

22 Abstract Data Types and Linked Lists 66922.1 Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66922.2 Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670

Fig. 22.1: An empty list (left) and a 3-element linked list (right). . . . . . . . . . . 67022.3 The Stack ADT and a Linked List Implementation . . . . . . . . . . . . . . . . . . . . . . . . 670

22.3.1 Implementation of a Stack in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671Fig. 22.2: The Stack interface file stack.h. . . . . . . . . . . . . . . . . . . . . . . 671Fig. 22.3: Stack implementation file, stack.c. . . . . . . . . . . . . . . . . . . . . . 672Fig. 22.4: Stack setup and cleanup. . . . . . . . . . . . . . . . . . . . . . . . . . . . 672Fig. 22.5: The Stack push function . . . . . . . . . . . . . . . . . . . . . . . . . . . 673Fig. 22.6: The Stack pop function . . . . . . . . . . . . . . . . . . . . . . . . . . . 674Fig. 22.7: Print the Stack data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674Fig. 22.8: Stack query functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675

22.3.2 Application of a Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676Fig. 22.9: Ball-matching game: The main program. . . . . . . . . . . . . . . . . . . 676Fig. 22.10: Ball-matching game: Second part of main.c. . . . . . . . . . . . . . . . . 676

23 Abstract Data Types and Linked Lists 67923.1 Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67923.2 Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680

Fig. 23.1: An empty list (left) and a 3-element linked list (right). . . . . . . . . . . 68023.3 The Stack ADT and a Linked List Implementation . . . . . . . . . . . . . . . . . . . . . . . . 680

23.3.1 Implementation of a Stack in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681Fig. 23.2: The Stack interface file stack.h. . . . . . . . . . . . . . . . . . . . . . . 681Fig. 23.3: Stack implementation file, stack.c. . . . . . . . . . . . . . . . . . . . . . 682Fig. 23.4: Stack setup and cleanup. . . . . . . . . . . . . . . . . . . . . . . . . . . . 682Fig. 23.5: The Stack push function . . . . . . . . . . . . . . . . . . . . . . . . . . . 683Fig. 23.6: The Stack pop function . . . . . . . . . . . . . . . . . . . . . . . . . . . 684Fig. 23.7: Print the Stack data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684Fig. 23.8: Stack query functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685

23.3.2 Application of a Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686Fig. 23.9: Ball-matching game: The main program. . . . . . . . . . . . . . . . . . . 686Fig. 23.10: Ball-matching game: Second part of main.c. . . . . . . . . . . . . . . . . 686

23.4 The Queue ADT and a Linked List Implementation . . . . . . . . . . . . . . . . . . . . . . . 68723.4.1 Implementation of a Queue in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687

Fig. 23.11: The Queue interface file queue.h. . . . . . . . . . . . . . . . . . . . . . . 687Fig. 23.12: Queue implementation file, queue.c. . . . . . . . . . . . . . . . . . . . . 687Fig. 23.13: Queue setup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687Fig. 23.14: Queue cleanup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687Fig. 23.15: The Queue push function . . . . . . . . . . . . . . . . . . . . . . . . . . 687Fig. 23.16: The Queue pop function . . . . . . . . . . . . . . . . . . . . . . . . . . . 687Fig. 23.17: Print the Queue data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687Fig. 23.18: Queue query functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687

23.4.2 Application of a Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688Fig. 23.19: Credit card transactions: transqueue.h. . . . . . . . . . . . . . . . . . . . 688Fig. 23.20: Reading a transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688Fig. 23.21: Printing a transaction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688Fig. 23.22: Credit card statements: The main program. . . . . . . . . . . . . . . . . 688

23.5 The Priority Queue ADT and a Sorted List Implementation . . . . . . . . . . . . . . . . . . . 68923.6 Managing Persistant Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68923.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689

Page 24: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

xxiv CONTENTS

A The ASCII Code 695

B The Precedence of Operators in C 697Fig. B.1: The precedence of operators in C. . . . . . . . . . . . . . . . . . . . . . . 697

C Keywords 699C.1 Preprocessor Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699C.2 Control Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699C.3 Types and Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699C.4 Additional C++ Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699C.5 An Alphabetical List of C and C++ Reserved Words . . . . . . . . . . . . . . . . . . . . . . . 700

D Advanced Aspects C Operators 701D.1 Assignment Combination Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701

Fig. D.1: Assignment combinations. . . . . . . . . . . . . . . . . . . . . . . . . . . 701Fig. D.2: A parse tree for assignment combinations. . . . . . . . . . . . . . . . . . 701

D.2 More on Lazy Evaluation and Skipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701Fig. D.3: Lazy evaluation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702Fig. D.4: Skip the whole operand. . . . . . . . . . . . . . . . . . . . . . . . . . . . 703Fig. D.5: And nothing but the operand. . . . . . . . . . . . . . . . . . . . . . . . . 704

D.2.1 Evaluation Order and Side-Effect Operators . . . . . . . . . . . . . . . . . . . . . . . . 704D.3 The Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705

Fig. D.6: A flowchart for the conditional operator. . . . . . . . . . . . . . . . . . . 705Fig. D.7: A tree for the conditional operator. . . . . . . . . . . . . . . . . . . . . . 706

D.4 The Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706D.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707

Fig. D.8: Complications in use of side-effect operators. . . . . . . . . . . . . . . . . 707

E Base Conversion 709E.1 To and from Binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709

Fig. E.1: Converting binary numbers to decimal. . . . . . . . . . . . . . . . . . . . 709Fig. E.2: Converting between hexadecimal and binary. . . . . . . . . . . . . . . . . 709Fig. E.3: Converting decimal numbers to binary. . . . . . . . . . . . . . . . . . . . 709

E.2 Decimal and Hexadecimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709Fig. E.4: Converting hexadecimal numbers to decimal. . . . . . . . . . . . . . . . 710

E.3 Self-Test Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711

F The Standard C Environment 713F.1 Built-in Facilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713F.2 Standard Files of Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713

Fig. F.1: Minimum values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714F.3 The Standard Libraries and main() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714

F.3.1 The Function main() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714F.3.2 Characters and Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715F.3.3 Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715F.3.4 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717F.3.5 Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719F.3.6 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720F.3.7 Time and Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722F.3.8 Variable-Length Argument Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723

F.4 Libraries Not Explored . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723

Page 25: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

Preface

Preface to the Second Edition

Changes. Students at the University of New Haven have been learning programming from this book forten years. During that time, some major things have changed. First, the instructors at UNH learned manyways, great and small, that the content and sequence of the book could be improved. Second, Java firstbecame the dominant language for teaching beginners, and is now losing that place because it is too complex.The teaching community in the United States recognizes that we should be introducing computing principlesthrough a language that is less extensive and less confusing. Third, the C-99 standard has been publishedand is in common use, bringing important improvements to the C language.

This edition improves the order of presentation of the first edition, adds several explanatory diagramsand examples, and incorporates important new features of C (such as type bool). Most of the examples andexercises that relate to engineering topics have been removed from the printed text and will be supplied ona book-website.

We have added a chapter on the creation and and use of linked lists, which is important for studentswho learned Java first and do not know how to use pointers to make data structures. Stephen Ross, one ofour original authors, has had no role in these revisions. A new author, Michael J. Fischer, has been activelyinvolved in the upgrade and in writing the new chapter.

Applied C continues to provide accurate instruction in C syntax and semantics and to give a firm basisfor a transition to object-oriented programming in C++ or Java as a second course.

Preface to the First Edition

How it all began. This book arose from our need to teach C programming to beginners at the University ofNew Haven, and from what we perceived as the lack of any appropriate elementary text to do this. Applied Cis the brainchild of Stephen Ross and Alice Fischer, the two original authors, who are professors of mechanicalengineering and computer science, respectively. David Eggert, an assistant professor of computer science,joined them midway through the project.

The blending of our talents has produced a book that is technically correct (well, nobody is perfect) andcomprehensive (due mostly to David’s thoroughness). It contains explanations that can be understood by abeginner (Alice has taught a lot of new programmers), and at the same time reflects the soul of an engineer(Steve’s that is), with its emphasis on precise specification, careful testing, complete examples, highly variedapplications, diagrams and visual aids of all sorts.

This text takes a “hands-on” approach: students are encouraged to learn from example and to beginprogramming immediately in a subset of the language. Simple programming skills are presented both byexposition and through many complete examples from the fields of mathematics, science and engineering.Familiarity with the core of the C language is developed through reading and writing basic programs. Fol-lowing this, each topic is revisited and information is presented in depth. This approach leads to greaterunderstanding.

xxv

Page 26: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

xxvi CONTENTS

Why C and not C++? Many universities are now teaching their first programming courses in C++, whilewe focus on C. There are many reasons why we believe C is more appropriate than C++ for an introduction toprogramming. One of these is evident in the names of the languages. C++ is C plus constructs for handlinglarge-scale complexity. One problem beginning students have is how to handle the wealth of informationthat is being thrown at them. While it is true that C++ has several advantages over C, many of these donot become evident until a program becomes larger and more complex. Then the object-oriented paradigmfunctions as a powerful organizational tool.

Ultimately, most computer science and engineering students need to be fluent in both C and C++. Ittherefore seems more natural to teach them C first, and build upon this knowledge when introducing C++.By then, they will have a better foundation for understanding abstract data types. And yet, even thoughwe are teaching in C, the programming style developed in the later chapters leads gradually but consistentlyinto the object-oriented paradigm.

In addition to these larger issues, there are smaller advantages of beginning in C. While it is true thatsome of the I/O in C++ (e.g., integers) is simpler than in C, other data types such as strings and floating-point values can actually be formatted more easily in C. Another important factor for beginners is the setof error and warning messages provided by a compiler during program development. While most of today’ssystems compile both C and C++ code, the quality of the error comments is usually worse for C++. Theyare often vague or just not understandable without greater knowledge. (The error comments in C are notperfect, either, but they are simpler.)

Should you use this book? Applied C is intended as an introduction to C programming for studentswho have not previously programmed. The text is also eminently suitable for those already familiar withother programming languages such as FORTRAN, Pascal, and Java. It has been in use at the Universityof New Haven since 1995 with students from the engineering, math and computer science fields, and theoccasional student from other scientific disciplines.

While it is not necessary for a student to be familiar with computers before using this book, he or sheshould have a mind-set that is receptive to the programming process. (We have found that students whohave strong math skills do best.) Many parallels can be drawn between the process of solving a word problemin a math text and devising a computer program to solve a problem. Since many of the examples used aredrawn from the engineering and mathematics fields, having a background which includes algebra, geometryand trigonometry can be very helpful. Of course, instructors can choose to supplement this material withother less sophisticated examples for audiences with lesser math backgrounds.

Scope and a roadmap. The text is quite comprehensive and contains enough material for two quartersof instruction. Topics in Parts 1 through 3 can be covered in one quarter or a trimester and are the mostimportant for students entering technical careers. A semester course can also cover most of Part 4. A secondquarter or trimester course would allow one to cover Parts 4 and 5 entirely. The nature of these parts is asfollows.

• Part 1: Introduction.

Chapters 1 and 2 cover computers, algorithms, program specifications, test plans, and the pro-gramming process. The first chapter on computers can be omitted easily if the instructor is pressedfor time or the students already have this background. Chapter 3 is a progressive series of annotatedprograms that introduces variables, the types double and int, simple input and output, arithmetic,the if...else conditional statement and the while loop. Emphasis is on learning by doing. By theend of Chapter 3, the student should be able to write a simple program that takes interactive input,validates it, performs a computation, and produces output.

• Part 2: Computation.

The next three chapters cover declarations, expressions, functions of zero and one parameter, usinglibraries, and the remaining control statements (for, do...while, switch, break, and continue).Emphasis is on safe programming practices and generating modular code. In Chapter 5, we introduce

Page 27: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS xxvii

the tools library, a small package of functions we have found useful when teaching this material. Itserves as a vehicle for demonstrating many different aspects of programming throughout the text. Bythe end of Chapter 6, the student should be able to write a program specification and a test plan, aswell as write and debug a program that includes calls on library and programmer-defined functions.

• Part 3: Basic Data Types.

Chapters 7 and 8 complete the coverage of primitive data types in C. Types float, short int,long int, and char are introduced, along with their I/O formats, specialized operators, library func-tions, and examples of appropriate usage. Several important issues are discussed, including numericoverflow, approximate representation of real values, and type conversions.

Chapter 9 extends the coverage of functions to include an arbitrary number of parameters of anycombination of types. It introduces a method for modular program development that is designed tominimize the terror a student feels when confronted by a blank page and a “story problem”. Stagesof analysis are prescribed, starting with an informal specification and testing plan. Then the studentis asked to write down the familiar and routine parts of a program, deferring parts that seem compli-cated at first. Gradually attention is focused on each deferred section, which is always simpler whenconsidered in isolation. Important issues discussed include function call charts, stub testing, scope andlifetime of variables, the proper use of parameters and local declarations and the bad effects of globalvariables.

Chapters 10 and 12 introduce the use of arrays, subscripts, and pointers. Issues include arrayparameters, simple array processing techniques, parallel arrays, walking on memory, and call-by-addressparameters. These chapters complete the coverage of the fundamental aspects of the C language. Atthe end of this material, the student should be able to program fluently in a restricted subset of thelanguage.

• Part 4: Structured Data Types.

Chapters 13 through 18 cover simple data structures and algorithms that normally are of generalinterest and commonly covered at the end of a one-semester course: namely, files, strings, structures,multi-dimensional arrays and bit vectors. We stop short of introducing more complex data structuresthat typically are collections of various structures via pointers (i.e., linked lists, trees and graphs).Instead, several additional topics are presented that are rarely given good textbook coverage: raggedarrays, arrays of structures, bit-level computation, hardware interfacing, streams, and I/O exceptionhandling. These topics provide a rich resource for a second quarter course or later self-study. Bythe end of this part, students will be transitioning from writing simple one page programs to thoserequiring the use of multiple functions to solve a problem properly.

• Part 5: Advanced Techniques.

Chapters 19 through 23 develop more sophisticated topics and provide a transition to the higherlevel of skill and knowledge needed by a student who will continue in a computer science major. Theypresent and carefully illustrate several standard algorithms for searching, sorting, finding roots ofequations, and simulation.

The topics and techniques presented in this section include some that are rarely covered in anaccessible manner by textbooks. This includes dynamic memory allocation, the use of pointers toprocess arrays, recursion, command-line arguments, the use of generic library functions like qsort(),and the construction and linking of multi-module programs. By the end of the text, students shouldbe able to write and debug a moderately long modular program and have the background and skillsneeded to begin a typical data structures course.

• Appendices: Useful information.

The appendices include standard tables (ASCII, precedence, C keywords), a glossary, and a sum-mary listing of the functions, data types, and constants found in the standard C libraries. We alsodescribe the complete contents of a local library, tools, that accompanies this text. Two more appen-dices give information that we deem to be beyond the scope of the main line of this text, but necessary

Page 28: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

xxviii CONTENTS

for completeness; these cover advanced aspects of some C operators and number representation andbase conversion. Lastly, Appendix I gives the answers to the Self-Test exercises.

The spiral approach to teaching. Unlike many programming books, this text is not organized as areference manual. In the spirit of the newer methods of teaching engineering and science, several topics areintroduced briefly, used in a restricted manner, and then considered in detail later. For example, arrays areintroduced in Chapter 10 and used in simple ways. More details, simple sorting and searching algorithmsand two-dimensional arrays are later explained in Chapters 10 and 18. Finally, dynamic array allocationand the use of pointers in array processing are presented in Chapters 16 and 17. This “as needed”, or “justin time” approach has many advantages:

• It motivates the student, because programs of more interest can be shown in the early chapters of thebook.

• It permits an early introduction of functions and arrays, but allows for a gradual development of theircomplexity in the programming examples and exercises.

• It eases the transition to the detailed explanations that occur in later chapters.

• It has an inductive quality that is very appropriate for the attitudes and skills needed by engineersand scientists: partial knowledge precedes full understanding, with much experimentation in between.

• The order in which major topics are presented has been tested in class and adjusted several times. Theresulting order given here works; students are ready for each topic when it comes, and professors areable to work within the bounds of the gradually increasing subset of C.

In many of the early chapters, certain sections are labelled with the terms “Advanced” or “Optional”.Those labelled as “Advanced” go into more depth on a topic than may really be necessary for basic un-derstanding. However, the material is relevant to some later section of the book, and it would be best ifthe topic was covered prior to reaching that point. Similar detailed material has been included in certainappendices or may be found in footnotes. Those topics labelled as “Optional” are just that. They containinformation which is relevant and should one day be learned by the student. Yet, these sections can beskipped without losing continuity in the text’s material.

Teaching design to the beginner. Modular design is stressed from the beginning, starting with the needfor a complete and accurate program specification, and the importance of a test plan and careful programverification. The design process is dealt with at three levels. In Chapter 5 we focus on using libraries andwriting simple functions. Chapter 9 extends and expands the basic principles of writing functions and dealswith top-down design. Finally, Chapter 21 introduces multi-module applications and the facilities providedby the C language, as well as typical operating systems, to support them. In the intervening chapters, manyexamples of top-down design are presented.

One of the authors teaches advanced courses in C++ and object-oriented design. All of the materialpresented here is developed with the assumption that students will eventually be programming in an object-oriented language. The program design techniques that are taught here will carry over gracefully into theobject-oriented environment. Stress is put on making everything highly modular and designing a programso that its functions correspond to the data structures that they process. In later chapters, the conceptsof encapsulation, privacy, objects with state, and reusable code are introduced and integrated into everyapplication. As mentioned earlier, we believe this is a more effective approach to teaching both C and C++than beginning with the more complex language.

A host of useful features. Topics are generally introduced by first providing some background andexplaining the importance of the topic. We hope this will motivate the reader and help maintain interest. Thisis followed by a careful exposition of the principles involved, written in accessible language and illustratedliberally with diagrams, graphs, and short programs that exercise the principles and demonstrate propersyntax. In addition, each chapter has a full complement of pedagogic aids:

Page 29: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

CONTENTS xxix

• Each major topic is illustrated by a complete program, with output and annotations that show therelevance of the program to the material at hand. These annotations refer to sections of the code whichhave been boxed and highlighted to further emphasize the use of the current topics.

• An application program or a case study is given that uses the techniques presented in the currentchapter. Each is developed using the modular design steps that have been discussed in the text.

• A summary section at the end of each chapter gives perspective on the most important conceptsintroduced. It can be a valuable study guide for the student or an aid to lecture preparation for theinstructor.

• Programming style hints and a list of frequent errors suggest a series of Do’s and Don’ts for thedeveloping programmer.

• A vocabulary list is also included in each chapter-end summary.

• A variety of self-test exercises are provided, with solutions that can be found in Appendix I. Similarexercises, without solutions, are given under the heading “Using Pencil and Paper”. These exercisesserve to test how well the student understands the theory of the material just presented.

• In addition, each chapter has a set of programming exercises that are highly varied in difficulty andcomplexity. Some problems present entirely new situations, while others ask the student to adapt orextend an existing program so that it uses a new technique.

• All code described or shown in the text is provided in electronic form on the web site www.mhhe.com//engcs//.Solutions to the Paper and Pencil exercises, as well as the programming problems, are available in elec-tronic form for instructors.

• Lastly, the web site contains a set of hints for the instructor on how to present the material in eachchapter.

Teaching responsible programming. A computer is a powerful tool. When we teach students howto use such a tool, we have an obligation to teach them responsible use of it and to help them develop aprofessional attitude toward programming. This philosophy is integrated into every part of this book asfollows:

• Part 1 stresses the importance of writing careful specifications before any coding begins, and doingadequate testing during the debugging process.

• Part 2 preaches and models an avoidance of techniques and language features that are prone to error.It demonstrates simple, safe, and effective methods that can be used instead. Fancier is not alwaysbetter.

• Part 3 stresses that we must understand the limitations of computers and develop mechanisms tocounteract them.

• Part 4 presents safe paradigms for using pointers and files, which can often be difficult topics for newprogrammers.

• Part 5 shows how to encapsulate a data type and provides the necessary background for participationin a team project.

Throughout, we are careful to avoid giving wrong information, or even misleading information in theguise of simplification. Similarly, we avoid solving problems by the use of poor designs or inappropriatetechniques. Where more than one viable alternative exists, we note this and explain why the chosen methodwas presented. At all times, we attempt to show only code that the student should emulate. Footnotes andforward references are used to inform the reader when a topic has aspects which are not being covered forthe time being, or maybe not at all.

Page 30: Applied Introductory C Programming - University of New Haveneliza.newhaven.edu/apc/parts/apc_TOC.pdf · Applied Introductory C Programming Alice E. Fischer David W. Eggert University

xxx CONTENTS

Many Thanks. We would first like to thank the many anonymous and not so anonymous reviewers whosecareful critiques have helped to shape the material in this text:

Jeanne Douglas, University of VermontGlynis Hamel, Worcester Polytechnic InstituteAbhijit Pandya, Florida Atlantic UniversityAlexander Pelin, Florida International UniversityJames Gips, Boston College.

We send our appreciation to the staff at McGraw-Hill, including the multiple project managers, who helpedmake this book a reality, when at times it seemed like it would never be finished.

We thank Trina Woo, Judi McDermott-Eggert, Noel, and Kate for their patience in putting up withmissed meals and long work hours.