Home > Store

Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C)

Register your product to gain access to bonus material or receive a coupon.

Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C)

eBook (Watermarked)

  • Your Price: $25.59
  • List Price: $31.99
  • Includes EPUB and PDF
  • About eBook Formats
  • This eBook includes the following formats, accessible from your Account page after purchase:

    ePub EPUB The open industry format known for its reflowable content and usability on supported mobile devices.

    Adobe Reader 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.


  • Copyright 2016
  • Dimensions: 7" x 9-1/8"
  • Pages: 384
  • Edition: 1st
  • eBook (Watermarked)
  • ISBN-10: 0-13-312441-X
  • ISBN-13: 978-0-13-312441-5

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

  • Setting up a C environment
  • Basic syntax and idioms
  • Compilation, make files, and linkers
  • Operators, variables, and data types
  • Program control
  • Arrays and strings
  • Functions, pointers, and structs
  • Memory allocation
  • I/O and files
  • Libraries
  • Data structures, including linked lists, sort, and search
  • Stacks and queues
  • Debugging, defensive coding, and automated testing
  • Fixing stack overflows, illegal memory access, and more
  • Breaking and hacking your own C code

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.

Sample Content

Table of Contents

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


Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020