Table of Contents


Preface . . . . . . . . . . . . . . . . . . . . . . . . . . .  xi

How to Use This Study Guide . . . . . . . . . . . . . . . . .  xi

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . .  xi

1    Introducing C  . . . . . . . . . . . . . . . . . . . . .   1
     1.1  Chapter Summary . . . . . . . . . . . . . . . . . .   1
     1.2  C and Other Languages . . . . . . . . . . . . . . .   1
          1.2.1  C and Pascal   . . . . . . . . . . . . . . .   1
          1.2.2  C and Fortran  . . . . . . . . . . . . . . .   2
          1.2.3  C and BASIC  . . . . . . . . . . . . . . . .   2
     1.3  Exercises . . . . . . . . . . . . . . . . . . . . .   4

2    C Fundamentals . . . . . . . . . . . . . . . . . . . . .   7
     2.1  Chapter Summary . . . . . . . . . . . . . . . . . .   7
     2.2  Solved Exercises  . . . . . . . . . . . . . . . . .   9
          2.2.1  Solved Exercise 1: Calculating Net Pay . . .   9
     2.3  Programming Exercises . . . . . . . . . . . . . . .  12

3    Formatted Input/Output . . . . . . . . . . . . . . . . .  15
     3.1  Chapter Summary . . . . . . . . . . . . . . . . . .  15
          3.1.1  The printf function  . . . . . . . . . . . .  15
          3.1.2  The scanf function . . . . . . . . . . . . .  15
     3.2  Solved Exercises  . . . . . . . . . . . . . . . . .  16
          3.2.1  Solved Exercise 1: Calculating Travel Time .  16
     3.3  Programming Exercises . . . . . . . . . . . . . . .  20

4    Expressions  . . . . . . . . . . . . . . . . . . . . . .  23
     4.1  Chapter Summary . . . . . . . . . . . . . . . . . .  23
     4.2  Questions and Answers . . . . . . . . . . . . . . .  25
     4.3  Programming Exercises . . . . . . . . . . . . . . .  26

5    Selection Statements . . . . . . . . . . . . . . . . . .  29
     5.1  Chapter Summary . . . . . . . . . . . . . . . . . .  29
          5.1.1  Logical Expressions  . . . . . . . . . . . .  29
          5.1.2  The if Statement . . . . . . . . . . . . . .  29
          5.1.3  The switch Statement . . . . . . . . . . . .  31
     5.2  Solved Exercises  . . . . . . . . . . . . . . . . .  31
          5.2.1  Solved Exercise 1: A Grading Program . . . .  31
     5.3  Programming Exercises . . . . . . . . . . . . . . .  37

6    Loops  . . . . . . . . . . . . . . . . . . . . . . . . .  41
     6.1  Chapter Summary . . . . . . . . . . . . . . . . . .  41
          6.1.1  The while Statement  . . . . . . . . . . . .  41
          6.1.2  The do Statement . . . . . . . . . . . . . .  41
          6.1.3  The for Statement  . . . . . . . . . . . . .  42
          6.1.4  Exiting from a Loop  . . . . . . . . . . . .  42
          6.1.5  The Null Statement . . . . . . . . . . . . .  43
     6.2  Solved Exercises  . . . . . . . . . . . . . . . . .  43
          6.2.1. Solved Exercise 1: Loan Amortization . . . .  43
          6.2.2  Solved Exercise 2: The Chess Knight's Moves   46
     6.3  Programming Exercises . . . . . . . . . . . . . . .  51

7    Basic Types  . . . . . . . . . . . . . . . . . . . . . .  55
     7.1  Chapter Summary . . . . . . . . . . . . . . . . . .  55
          7.1.1 Integer Types . . . . . . . . . . . . . . . .  55
          7.1.2 Floating Types  . . . . . . . . . . . . . . .  56
          7.1.3 Character Types . . . . . . . . . . . . . . .  56
          7.1.4 The sizeof Operator . . . . . . . . . . . . .  56
          7.1.5 Type Conversion . . . . . . . . . . . . . . .  57
          7.1.6 Type Definitions  . . . . . . . . . . . . . .  57
     7.2  Solved Exercises  . . . . . . . . . . . . . . . . .  58
          7.2.1 Solved Exercise 1: A Word Analysis and
                Counting Program  . . . . . . . . . . . . . .  58
     7.3  Programming Exercises . . . . . . . . . . . . . . .  63

8    Arrays . . . . . . . . . . . . . . . . . . . . . . . . .  67
     8.1  Chapter Summary . . . . . . . . . . . . . . . . . .  67
          8.1.1 One-Dimensional Arrays  . . . . . . . . . . .  67
          8.1.2 Multidimensional Arrays . . . . . . . . . . .  68
     8.2  Solved Exercises  . . . . . . . . . . . . . . . . .  69
          8.2.1 Solved Exercise 1: Very Large Numbers . . . .  69
          8.2.2 Solved Exercise 2: Sorting  . . . . . . . . .  73
     8.3  Programming Exercises . . . . . . . . . . . . . . .  75

9    Functions  . . . . . . . . . . . . . . . . . . . . . . .  81
     9.1  Chapter Summary . . . . . . . . . . . . . . . . . .  81
          9.1.1 Defining and Calling Functions  . . . . . . .  81
          9.1.2 Function Declarations . . . . . . . . . . . .  83
          9.1.3 Arguments . . . . . . . . . . . . . . . . . .  83
          9.1.4 The return Statement  . . . . . . . . . . . .  84
          9.1.5 Program Termination . . . . . . . . . . . . .  84
          9.1.6 Recursive Functions . . . . . . . . . . . . .  84
     9.2  Solved Exercises  . . . . . . . . . . . . . . . . .  85
          9.2.1 Solved Exercise 1: Plotting the Sine Function  85
          9.2.2 Solved Exercise 2: Recursive Sorting  . . . .  89
     9.3  Programming Exercises . . . . . . . . . . . . . . .  91

10   Program Organization . . . . . . . . . . . . . . . . . .  95
     10.1 Chapter Summary . . . . . . . . . . . . . . . . . .  95
          10.1.1 Local Variables  . . . . . . . . . . . . . .  95
          10.1.2 External Variables . . . . . . . . . . . . .  95
          10.1.3 Blocks and Scope . . . . . . . . . . . . . .  95
          10.1.4 Organizing a C Program . . . . . . . . . . .  95
     10.2 Solved Exercises  . . . . . . . . . . . . . . . . .  96
          10.2.1 Solved Exercise 1: Sets of Numbers . . . . .  96
     10.3 Programming Exercises . . . . . . . . . . . . . . .  98

11   Pointers . . . . . . . . . . . . . . . . . . . . . . . . 103
     11.1 Chapter Summary . . . . . . . . . . . . . . . . . . 103
     11.2 Solved Exercises  . . . . . . . . . . . . . . . . . 105
          11.2.1 Solved Exercise 1: Complex Numbers . . . . . 105
     11.3 Programming Exercises . . . . . . . . . . . . . . . 107

12   Pointers and Arrays  . . . . . . . . . . . . . . . . . . 111
     12.1 Chapter Summary . . . . . . . . . . . . . . . . . . 111
     12.2 Solved Exercises  . . . . . . . . . . . . . . . . . 113
          12.2.1 Solved Exercise 1: Matrix Multiplication . . 113
     12.3 Programming Exercises . . . . . . . . . . . . . . . 117

13   Strings  . . . . . . . . . . . . . . . . . . . . . . . . 119
     13.1 Chapter Summary . . . . . . . . . . . . . . . . . . 119
          13.1.1 String Basics  . . . . . . . . . . . . . . . 119
          13.1.2 String Operations  . . . . . . . . . . . . . 120
          13.1.3 Arrays of Strings  . . . . . . . . . . . . . 120
     13.2 Solved Exercises  . . . . . . . . . . . . . . . . . 121
          13.2.1 Solved Exercise 1: Indenting a C Program . . 121
     13.3 Programming Exercises . . . . . . . . . . . . . . . 124

14   The Preprocessor . . . . . . . . . . . . . . . . . . . . 127
     14.1 Chapter Summary . . . . . . . . . . . . . . . . . . 127
          14.1.1 Macro Definition . . . . . . . . . . . . . . 127
          14.1.2 Conditional Compilation  . . . . . . . . . . 129
     14.2 Solved Exercises  . . . . . . . . . . . . . . . . . 130
          14.2.1 Solved Exercise 1: Macros versus Functions . 130
          14.2.2 Solved Exercise 2: Stacks of Different Types 132
     14.3 Programming Exercises . . . . . . . . . . . . . . . 134

15   Writing Large Programs . . . . . . . . . . . . . . . . . 137
     15.1 Chapter Summary . . . . . . . . . . . . . . . . . . 137
          15.1.1 Header Files . . . . . . . . . . . . . . . . 137
          15.1.2 Building a Multiple-File Program . . . . . . 139
     15.2 Solved Exercises  . . . . . . . . . . . . . . . . . 140
          15.2.1 Solved Exercise 1: Sets of Numbers . . . . . 140
     15.3 Programming Exercises . . . . . . . . . . . . . . . 146

16   Structures, Unions, and Enumerations . . . . . . . . . . 151
     16.1 Chapter Summary . . . . . . . . . . . . . . . . . . 151
          16.1.1 Structures . . . . . . . . . . . . . . . . . 151
          16.1.2 Unions and Enumerations  . . . . . . . . . . 153
     16.2 Solved Exercises  . . . . . . . . . . . . . . . . . 155
          16.2.1 Solved Exercise 1: A Bank Simulation . . . . 155
     16.3 Programming Exercises . . . . . . . . . . . . . . . 161

17   Advanced Uses of Pointers  . . . . . . . . . . . . . . . 165
     17.1 Chapter Summary . . . . . . . . . . . . . . . . . . 165
          17.1.1 Dynamic Storage  . . . . . . . . . . . . . . 165
          17.1.2 Linked Lists . . . . . . . . . . . . . . . . 167
          17.1.3 Pointers to Functions  . . . . . . . . . . . 168
     17.2 Solved Exercises  . . . . . . . . . . . . . . . . . 169
          17.2.1 Solved Exercise 1: Sets (Revisited). . . . . 169
          17.2.2 Solved Exercise 2: Numeric Integration . . . 174
     17.3 Programming Exercises . . . . . . . . . . . . . . . 176

18   Declarations . . . . . . . . . . . . . . . . . . . . . . 179
     18.1 Chapter Summary . . . . . . . . . . . . . . . . . . 179
          18.1.1 Storage Classes and Type Qualifiers. . . . . 179
          18.1.2 Declarators and Initializers . . . . . . . . 180
     18.2 Solved Exercises  . . . . . . . . . . . . . . . . . 181
          18.2.1 Solved Exercise 1: Arrays of Pointers
                 to Functions . . . . . . . . . . . . . . . . 181
     18.3 Programming Exercises . . . . . . . . . . . . . . . 183

19   Program Design . . . . . . . . . . . . . . . . . . . . . 185
     19.1 Chapter Summary . . . . . . . . . . . . . . . . . . 185
          19.1.1 Modules, Information Hiding, and Abstract
                 Data Types . . . . . . . . . . . . . . . . . 185
          19.1.2 C++  . . . . . . . . . . . . . . . . . . . . 187
     19.2 Solved Exercises  . . . . . . . . . . . . . . . . . 190
          19.2.1 Solved Exercise 1: A Stack Program in C
                 and in C++ . . . . . . . . . . . . . . . . . 190
          19.2.2 Solved Exercise 2: The ADT Car . . . . . . . 191
     19.3 Programming Exercises . . . . . . . . . . . . . . . 194

20   Low-Level Programming  . . . . . . . . . . . . . . . . . 197
     20.1 Chapter Summary . . . . . . . . . . . . . . . . . . 197
          20.1.1   Bitwise Operators  . . . . . . . . . . . . 197
          20.1.2   Bit-Fields in Structures . . . . . . . . . 197
     20.2 Solved Exercises  . . . . . . . . . . . . . . . . . 189
          20.2.1   Solved Exercise 1: Hamming Codes . . . . . 189
     20.3 Programming Exercises . . . . . . . . . . . . . . . 202

21   The Standard Library . . . . . . . . . . . . . . . . . . 205
     21.1 Chapter Summary . . . . . . . . . . . . . . . . . . 205
     21.2 Solved Exercises  . . . . . . . . . . . . . . . . . 206
          21.2.1 Solved Exercise 1: Holes in a Structure. . . 206
     21.3 Programming Exercises . . . . . . . . . . . . . . . 206

22   Input/Output . . . . . . . . . . . . . . . . . . . . . . 209
     22.1 Chapter Summary . . . . . . . . . . . . . . . . . . 209
          22.1.1 Streams  . . . . . . . . . . . . . . . . . . 209
          22.1.2 Formatted I/O  . . . . . . . . . . . . . . . 210
          22.1.3 Character, Line, Block, and String I/O . . . 211
     22.2 Solved Exercises  . . . . . . . . . . . . . . . . . 212
          22.2.1 Solved Exercise 1: A Bank Simulation
                 (Revisited). . . . . . . . . . . . . . . . . 212
          22.2.2 Solved Exercise 2: A Parts Database Program. 214
     22.3 Programming Exercises . . . . . . . . . . . . . . . 217

23   Library Support for Numbers and Character Data . . . . . 219
     23.1 Chapter Summary . . . . . . . . . . . . . . . . . . 219
     23.2 Solved Exercises  . . . . . . . . . . . . . . . . . 221
          23.2.1 Solved Exercise 1: A Text Formatter
                 (Revisited)  . . . . . . . . . . . . . . . . 221
          23.2.2 Solved Exercise 2: A Tokenizer Program . . . 222
     23.3 Programming Exercises . . . . . . . . . . . . . . . 223

24   Error Handling . . . . . . . . . . . . . . . . . . . . . 225
     24.1 Chapter Summary . . . . . . . . . . . . . . . . . . 225
     24.2 Solved Exercises  . . . . . . . . . . . . . . . . . 226
          24.2.1 Solved Exercise 1: Efficient String Search . 226
          24.2.2 Solved Exercise 2: An Interruptible File
                 Printer  . . . . . . . . . . . . . . . . . . 230
     24.3 Programming Exercises . . . . . . . . . . . . . . . 231

25   International Features . . . . . . . . . . . . . . . . . 233
     25.1 Chapter Summary . . . . . . . . . . . . . . . . . . 233
     25.2 Solved Exercises  . . . . . . . . . . . . . . . . . 235
          25.2.1 Solved Exercise 1: Printing Locale
                 Information  . . . . . . . . . . . . . . . . 235
     25.3 Programming Exercises . . . . . . . . . . . . . . . 236

26   Miscellaneous Library Functions  . . . . . . . . . . . . 237
     26.1 Chapter Summary . . . . . . . . . . . . . . . . . . 237
          26.1.1 Variable Argument Lists  . . . . . . . . . . 237
          26.1.2 General Utilities  . . . . . . . . . . . . . 237
          26.1.3 The <time.h> header  . . . . . . . . . . . . 238
     26.2 Solved Exercises  . . . . . . . . . . . . . . . . . 239
          26.2.1 Solved Exercise 1: Sorting Random Numbers  . 239
     26.3 Programming Exercises . . . . . . . . . . . . . . . 242