0% found this document useful (0 votes)
88 views10 pages

Intermediate C Book

Uploaded by

b'stard/tempest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views10 pages

Intermediate C Book

Uploaded by

b'stard/tempest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Yung-Hsiang Lu

Intermediate C Programming
Contents

List of Figures xi

List of Tables xvii

Foreword xix

Preface xxi

Author, Reviewers, and Artist xxv

Rules in Software Development xxvii

Source Code xxix

I Computer Storage: Memory and File 1


1 Program Execution 3
1.1 Compile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Redirect Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2 Stack Memory 9
2.1 Values and Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3 The Call Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3.1 The Return Location . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3.2 Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3.3 Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.3.4 Value Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3.5 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.3.6 Retrieving Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.4 Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.5.1 Draw Call Stack I . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.5.2 Draw Call Stack II . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.5.3 Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.6 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.6.1 Draw Call Stack I . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.6.2 Draw Call Stack II . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.6.3 Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.7 Examine the Call Stack with DDD . . . . . . . . . . . . . . . . . . . . . . . 27

iii
iv Contents

3 Prevent, Detect, and Remove Bugs 33


3.1 Developing Software �= Coding . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.1.1 Before coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.1.2 During coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.1.3 After coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.2 Common Mistakes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.2.1 Uninitialized Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.2.2 Wrong Array Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.2.3 Wrong Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.3 Post-Execution and Interactive Debugging . . . . . . . . . . . . . . . . . . 36
3.4 Separate Testing Code from Production Code . . . . . . . . . . . . . . . . 37

4 Pointers 39
4.1 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.2 The Swap Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.3 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.4 The Swap Function Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.5 Type Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
4.6 Arrays and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.7 Type Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.8 Pointer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.9.1 Swap Function 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.9.2 Swap Function 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.9.3 Swap Function 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.9.4 Swap Function 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.9.5 Swap Function 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.9.6 15,552 Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.10 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.10.1 Swap Function 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.10.2 Swap Function 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.10.3 Swap Function 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.10.4 Swap Function 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.10.5 Swap Function 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

5 Writing and Testing Programs 65


5.1 Distinct Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.1.1 main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
5.1.2 areDistinct Function . . . . . . . . . . . . . . . . . . . . . . . . . . 67
5.1.3 Compiling and Linking . . . . . . . . . . . . . . . . . . . . . . . . . . 68
5.1.4 make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
5.2 Test Using Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
5.2.1 Generating Test Cases . . . . . . . . . . . . . . . . . . . . . . . . . . 72
5.2.2 Redirecting Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
5.2.3 Use diff to Compare Output . . . . . . . . . . . . . . . . . . . . . . 73
5.2.4 Adding Tests to Makefile . . . . . . . . . . . . . . . . . . . . . . . . 73
5.3 Invalid Memory Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
5.4 Using valgrind to Check Memory Access Errors . . . . . . . . . . . . . . . 77
5.5 Test Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.6 Limit Core Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
5.7 Programs with Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Contents v

6 Strings 85
6.1 Array of Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
6.2 String Functions in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
6.2.1 Copy: strcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
6.2.2 Compare: strcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.2.3 Finding Substrings: strstr . . . . . . . . . . . . . . . . . . . . . . . 90
6.2.4 Finding Characters: strchr . . . . . . . . . . . . . . . . . . . . . . . 90
6.3 Understanding argv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
6.4 Counting Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

7 Programming Problems and Debugging 97


7.1 Implementing String Functions . . . . . . . . . . . . . . . . . . . . . . . . . 97
7.1.1 The C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
7.1.2 Header File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
7.1.3 mystring.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
7.1.4 Creating Inputs and Correct Outputs . . . . . . . . . . . . . . . . . 100
7.1.5 Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
7.1.6 mystring.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
7.1.7 Using const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
7.2 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
7.2.1 Find Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
7.2.2 Find Invalid Memory Accesses . . . . . . . . . . . . . . . . . . . . . 109
7.2.3 Detect Invalid Memory Accesses . . . . . . . . . . . . . . . . . . . . 111

8 Heap Memory 113


8.1 Creating Array with malloc . . . . . . . . . . . . . . . . . . . . . . . . . . 113
8.2 The Stack and the Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
8.3 Functions that Return a Heap Address . . . . . . . . . . . . . . . . . . . . 118
8.4 Two-Dimensional Arrays in C . . . . . . . . . . . . . . . . . . . . . . . . . 119
8.5 Pointers and Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

9 Programming Problems Using Heap Memory 125


9.1 Sorting an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
9.1.1 Generating Test Input and Expected Output . . . . . . . . . . . . . 125
9.1.2 Redirecting Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
9.1.3 Sorting Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
9.1.4 Using valgrind to Detect Memory Leaks . . . . . . . . . . . . . . . 132
9.2 Sort Using qsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
9.2.1 qsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
9.2.2 The Comparison Function . . . . . . . . . . . . . . . . . . . . . . . . 135
9.2.3 Execution Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
9.2.4 Sorting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

10 Reading and Writing Files 141


10.1 Passing a File Name via argv . . . . . . . . . . . . . . . . . . . . . . . . . 141
10.2 Reading from Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
10.2.1 Reading Characters: fgetc . . . . . . . . . . . . . . . . . . . . . . . 142
10.2.2 Reading Integers: fscanf(... %d...) . . . . . . . . . . . . . . . . . 145
10.3 Writing to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
10.4 Reading and Writing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . 150
vi Contents

11 Programming Problems Using File 153


11.1 Sorting a File of Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
11.2 Counting the Occurrences of Characters . . . . . . . . . . . . . . . . . . . . 155
11.3 Counting the Occurrences of a Word . . . . . . . . . . . . . . . . . . . . . . 158
11.4 How to Comment Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

II Recursion 163
12 Recursion 165
12.1 Selecting Balls with Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 166
12.1.1 Balls of Two Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
12.1.2 Balls of Three Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
12.1.3 A Further Restriction . . . . . . . . . . . . . . . . . . . . . . . . . . 168
12.2 One-Way Streets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
12.3 The Tower of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
12.4 Calculating Integer Partitions . . . . . . . . . . . . . . . . . . . . . . . . . 174
12.4.1 Count the Number of “1”s . . . . . . . . . . . . . . . . . . . . . . . . 175
12.4.2 Odd Numbers Only . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
12.4.3 Increasing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
12.4.4 Alternating Odd and Even Numbers . . . . . . . . . . . . . . . . . . 179
12.4.5 Generalizing the Integer Partition Problem . . . . . . . . . . . . . . 180
12.4.6 How Not to Solve the Integer Partition Problem . . . . . . . . . . . 181

13 Recursive C Functions 183


13.1 Select Balls with Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . 184
13.2 One-Way Streets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
13.3 The Tower of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
13.4 Integer Partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
13.5 Factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
13.6 Fibonacci Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
13.7 Performance Profiling with gprof . . . . . . . . . . . . . . . . . . . . . . . 199

14 Integer Partition 201


14.1 Stack and Heap Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
14.2 Trace Recursive Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . 210
14.3 Generating Partitions with Restrictions . . . . . . . . . . . . . . . . . . . . 213
14.3.1 Using Odd Numbers Only . . . . . . . . . . . . . . . . . . . . . . . . 214
14.3.2 Using Sequences of Increasing Numbers . . . . . . . . . . . . . . . . 215
14.3.3 Using Alternating Odd and Even Numbers . . . . . . . . . . . . . . 215
14.3.4 Using gprof and gcov to Identify Performance Bottlenecks . . . . . 216

15 Programming Problems Using Recursion 223


15.1 Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
15.2 Quick Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
15.3 Permutations and Combinations . . . . . . . . . . . . . . . . . . . . . . . . 232
15.4 Stack Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
15.4.1 Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
15.4.2 Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
15.4.3 Example 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
15.4.4 Example 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
15.4.5 Stack Sortable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Contents vii

15.5 Tracing a Recursive Function . . . . . . . . . . . . . . . . . . . . . . . . . . 242


15.6 A Recursive Function with a Mistake . . . . . . . . . . . . . . . . . . . . . 244

III Structure 247


16 Programmer-Defined Data Types 249
16.1 Struct and Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
16.2 Passing Objects as Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 253
16.3 Objects and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
16.3.1 Returning an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
16.3.2 Objects and malloc . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
16.4 Constructors and Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . 261
16.5 Structures within Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 268
16.6 Binary Files and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

17 Programming Problems Using Structure 275


17.1 Sorting a Person Database . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
17.2 Packing Decimal Digits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
17.2.1 Number Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
17.2.2 Packing Two Decimal Digits into One Byte . . . . . . . . . . . . . . 282
17.2.3 Bit Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
17.2.4 Inserting and Retrieving Decimal Digits . . . . . . . . . . . . . . . . 285
17.2.5 DecPack Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
17.3 Binary File and Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

18 Linked Lists 293


18.1 Expandable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
18.2 Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
18.3 Inserting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
18.4 Searching a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
18.5 Deleting from a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
18.6 Printing a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
18.7 Destroying a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

19 Programming Problems Using Linked List 307


19.1 Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
19.2 Sorting Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
19.3 Sparse Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
19.4 Reversing a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314

20 Binary Search Trees 317


20.1 Binary Search Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
20.2 Inserting Data into a Binary Search Tree . . . . . . . . . . . . . . . . . . . 319
20.3 Searching a Binary Search Tree . . . . . . . . . . . . . . . . . . . . . . . . 323
20.4 Printing a Binary Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
20.5 Deleting from a Binary Search Tree . . . . . . . . . . . . . . . . . . . . . . 327
20.6 Destroying a Binary Search Tree . . . . . . . . . . . . . . . . . . . . . . . . 330
20.7 main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
20.8 Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
20.9 Counting the Different Shapes of a Binary Tree . . . . . . . . . . . . . . . 332
viii Contents

21 Parallel Programming Using Threads 335


21.1 Parallel Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
21.2 Multi-Tasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
21.3 POSIX Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
21.4 Subset Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
21.4.1 Generate Test Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
21.4.2 Sequential Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
21.4.3 Multi-Threaded Solution . . . . . . . . . . . . . . . . . . . . . . . . . 345
21.5 Interleaving the Execution of Threads . . . . . . . . . . . . . . . . . . . . . 348
21.6 Thread Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
21.7 Amdahl’s Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356

IV Applications 357
22 Finding the Exit of a Maze 359
22.1 Maze File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
22.2 Reading the Maze File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
22.3 The Maze Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
22.4 An Escape Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
22.5 Implementing the Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
22.5.1 canMove Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
22.5.2 getOut Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
22.5.3 Printing Visited Locations . . . . . . . . . . . . . . . . . . . . . . . . 378

23 Image Processing 381


23.1 Structure for Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
23.2 Processing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
23.2.1 Image Pixels and Colors . . . . . . . . . . . . . . . . . . . . . . . . . 388
23.2.2 Processing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
23.2.3 Applying a Color Filter . . . . . . . . . . . . . . . . . . . . . . . . . 389
23.2.4 Inverting the Image Colors . . . . . . . . . . . . . . . . . . . . . . . 389
23.2.5 Edge Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
23.2.6 Color Equalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

24 Huffman Compression 395


24.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
24.2 Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
24.2.1 Count Frequencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
24.2.2 Sort by Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
24.2.3 Build a Code Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
24.2.4 Build a Code Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
24.2.5 Compress a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
24.2.6 Compress with Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
24.3 Decoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423

A Linux 443
A.1 Options for Installing Linux . . . . . . . . . . . . . . . . . . . . . . . . . . 443
A.2 Getting Ubuntu Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
A.3 Downloading and Installing VirtualBox . . . . . . . . . . . . . . . . . . . . 445
A.4 Install and Update Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
A.5 Install Programming Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Contents ix

B Version Control 447


B.1 Github.com . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
B.2 Cloning a Repository and Modifying a File . . . . . . . . . . . . . . . . . . 447
B.3 Adding Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . 449
B.4 Revising a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449

C Integrated Development Environments (IDE) 451


C.1 Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
C.2 Create and Build a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
C.3 Debugging the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454

Index 461

Recommendations 467
Recommendations

Harald Smit (Software Manager)


Intermediate C Programming bridges that critical gap between beginner and expert
with clear examples in key areas. This book covers important concepts we use every day in
industry when developing and debugging code.

David B. Nelson, Ph.D. (Associate Director, Center for Instruc-


tional Excellence, Purdue University):
Higher order cognition occurs when one can analyze disparate parts of problems and
issues or perform complicated operations. But advanced, critical thinking requires an assess-
ment of how negative consequences can be avoided. In computer programming education,
the leap between beginner-level recognition of syntax and artful, efficient language author-
ing occurs only when a student can regularly identify and predict likely errors in authored
code. Intermediate Programming provides essential lessons and practice in error analysis.
By prioritizing debugging into each lesson, the author compels learners to consider the
consequences of coding choices, one block at a time.

Siau Cheng Khoo, Ph.D. (National University of Singapore):


This well-written book provides the necessary tools and practical skills to turn students
into seasoned programmers. It does not only teach students how to write good programs,
but, more uniquely, also teach them how to avoid writing bad programs. The inclusion of
Linux operations and Versioning control, as well as the coverage of applications and IDE,
builds up students’ confidence in taking control over large scale software developments . At
the end of this learning journey, students will possess the skill for helping others to debug
their programs, an important step for building a new generation of programmers who are
able to help one another in software development.

467
468 Recommendations

Niklas Elmqvist, Ph.D. (Associate Professor and Program Director,


Master of Science in Human-Computer Interaction, University of
Maryland)
This book is unique in that it covers the C programming language from a bottom-up
perspective, which is rare in programming books. Instead of starting with the high-level
concepts, which easily gets dry and uninspiring for students, the book begins with practical
problems and progressively introduces the C concepts necessary to solve those problems.
This means that students immediately understand how the language works from a very
practical and pragmatic perspective.

You might also like