SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
You Will Learn C!
Zed Shaw has crafted the perfect course for the beginning C programmer eager to advance their skills in any language. Follow it and you will learn the many skills early and junior programmers need to succeed–just like the hundreds of thousands of programmers Zed has taught to date! You bring discipline, commitment, persistence, and experience with any programming language; the author supplies everything else.
In Learn C the Hard Way, you’ll learn C by working through 52 brilliantly crafted exercises. Watch Zed Shaw’s teaching video and read the exercise. Type his code precisely. (No copying and pasting!) Fix your mistakes. Watch the programs run. As you do, you’ll learn what good, modern C programs look like; how to think more effectively about code; and how to find and fix mistakes far more efficiently. Most importantly, you’ll master rigorous defensive programming techniques, so you can use any language to create software that protects itself from malicious activity and defects.
Through practical projects you’ll apply what you learn to build confidence in your new skills. Shaw teaches the key skills you need to start writing excellent C software, including
It’ll Be Hard at First. But Soon, You’ll Just Get It–And That Will Feel Great!
This tutorial will reward you for every minute you put into it. Soon, you’ll know one of the world’s most powerful programming languages. You’ll be a C programmer.
Acknowledgments xiv
This Book Is Not Really about C xv
The Undefined Behaviorists xvi
C Is a Pretty and Ugly Language xvii
What You Will Learn xviii
How to Read This Book xviii
The Videos xix
Exercise 0: The Setup 2
Linux 2
Mac OS X 2
Windows 3
Text Editor 3
Exercise 1: Dust Off That Compiler 6
Breaking It Down 6
What You Should See 7
How to Break It 8
Extra Credit 8
Exercise 2: Using Makefiles to Build 10
Using Make 10
What You Should See 11
How to Break It 12
Extra Credit 12
Exercise 3: Formatted Printing 14
What You Should See 15
External Research 15
How to Break It 15
Extra Credit 16
Exercise 4: Using a Debugger 18
GDB Tricks 18
GDB Quick Reference 18
LLDB Quick Reference 19
Exercise 5: Memorizing C Operators 20
How to Memorize 20
The List of Operators 21
Exercise 6: Memorizing C Syntax 26
The Keywords 26
Syntax Structures 27
A Word of Encouragement 30
A Word of Warning 31
Exercise 7: Variables and Types 32
What You Should See 34
How to Break It 34
Extra Credit 34
Exercise 8: If, Else-If, Else 36
What You Should See 37
How to Break It 37
Extra Credit 38
Exercise 9: While-Loop and Boolean Expressions 40
What You Should See 40
How to Break It 41
Extra Credit 41
Exercise 10: Switch Statements 42
What You Should See 43
How to Break It 44
Extra Credit 44
Exercise 11: Arrays and Strings 46
What You Should See 47
How to Break It 48
Extra Credit 48
Exercise 12: Sizes and Arrays 50
What You Should See 51
How to Break It 52
Extra Credit 53
Exercise 13: For-Loops and Arrays of Strings 54
What You Should See 56
Understanding Arrays of Strings 56
How to Break It 57
Extra Credit 57
Exercise 14: Writing and Using Functions 58
What You Should See 59
How to Break It 60
Extra Credit 60
Exercise 15: Pointers, Dreaded Pointers 62
What You Should See 64
Explaining Pointers 65
Practical Pointer Usage 66
The Pointer Lexicon 66
Pointers Aren’t Arrays 67
How to Break It 67
Extra Credit 67
Exercise 16: Structs and Pointers to Them 68
What You Should See 71
Explaining Structures 71
How to Break It 72
Extra Credit 72
Exercise 17: Heap and Stack Memory Allocation 74
What You Should See 79
Heap versus Stack Allocation 80
How to Break It 81
Extra Credit 82
Exercise 18: Pointers to Functions 84
What You Should See 88
How to Break It 88
Extra Credit 89
Exercise 19: Zed’s Awesome Debug Macros 90
The C Error-Handling Problem 90
The Debug Macros 91
Using dbg.h 93
What You Should See 95
How the CPP Expands Macros 96
Extra Credit 98
Exercise 20: Advanced Debugging Techniques 100
Debug Printing versus GDB 100
A Debugging Strategy 101
Extra Credit 102
Exercise 21: Advanced Data Types and Flow Control 104
Available Data Types 104
Available Operators 108
Available Control Structures 110
Extra Credit 111
Exercise 22: The Stack, Scope, and Globals 112
ex22.h and ex22.c 112
ex22_main.c 114
What You Should See 117
Scope, Stack, and Bugs 118
How to Break It 119
Extra Credit 119
Exercise 23: Meet Duff’s Device 120
What You Should See 124
Solving the Puzzle 124
Extra Credit 125
Exercise 24: Input, Output, Files 126
What You Should See 128
How to Break It 129
The I/O Functions 129
Extra Credit 130
Exercise 25: Variable Argument Functions 132
What You Should See 135
How to Break It 136
Extra Credit 136
Exercise 26: Project logfind 138
The logfind Specification 138
Exercise 27: Creative and Defensive Programming 140
The Creative Programmer Mind-Set 140
The Defensive Programmer Mind-Set 141
The Eight Defensive Programmer Strategies 141
Applying the Eight Strategies 142
Order Is Not Important 149
Extra Credit 150
Exercise 28: Intermediate Makefiles 152
The Basic Project Structure 152
Makefile 153
What You Should See 159
Extra Credit 159
Exercise 29: Libraries and Linking 160
Dynamically Loading a Shared Library 161
What You Should See 163
How to Break It 164
Extra Credit 164
Exercise 30: Automated Testing 166
Wiring Up the Test Framework 167
Extra Credit 171
Exercise 31: Common Undefined Behavior 172
UB 20 173
Exercise 32: Double Linked Lists 174
What Are Data Structures 178
Making the Library 178
Doubly Linked Lists 179
Tests 185
What You Should See 187
How to Improve It 188
Extra Credit 188
Exercise 33: Linked List Algorithms 190
Bubble and Merge Sort 190
The Unit Test 191
The Implementation 193
What You Should See 195
How to Improve It 196
Extra Credit 197
Exercise 34: Dynamic Array 198
Advantages and Disadvantages 205
How to Improve It 206
Extra Credit 206
Exercise 35: Sorting and Searching 208
Radix Sort and Binary Search 211
How to Improve It 221
Extra Credit 222
Exercise 36: Safer Strings 224
Why C Strings Were a Horrible Idea 224
Using bstrlib 225
Learning the Library 226
Exercise 37: Hashmaps 228
The Unit Test 235
How to Improve It 238
Extra Credit 238
Exercise 38: Hashmap Algorithms 240
What You Should See 245
How to Break It 246
Extra Credit 247
Exercise 39: String Algorithms 248
What You Should See 255
Analyzing the Results 257
Extra Credit 258
Exercise 40: Binary Search Trees 260
How to Improve It 273
Extra Credit 273
Exercise 41: Project devpkg 274
What Is devpkg? 274
Project Layout 277
The Makefile 277
The Source Files 278
The Final Challenge 295
Exercise 42: Stacks and Queues 296
What You Should See 299
How to Improve It 299
Extra Credit 299
Exercise 43: A Simple Statistics Engine 300
Rolling Standard Deviation and Mean 300
Implemention 301
How to Use It 306
Extra Credit 307
Exercise 44: Ring Buffer 310
The Unit Test 313
What You Should See 313
How to Improve It 314
Extra Credit 314
Exercise 45: A Simple TCP/IP Client 316
Augment the Makefile 316
The netclient Code 316
What You Should See 320
How to Break It 320
Extra Credit 321
Exercise 46: Ternary Search Tree 322
Advantages and Disadvantages 330
How to Improve It 331
Extra Credit 331
Exercise 47: A Fast URL Router 332
What You Should See 335
How to Improve It 335
Extra Credit 336
Exercise 48: A Simple Network Server 338
The Specification 338
Exercise 49: A Statistics Server 340
Specification 340
Exercise 50: Routing the Statistics 342
Exercise 51: Storing the Statistics 344
The Specification 344
Exercise 52: Hacking and Improving Your Server 346
Next Steps 348
Index 349