O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming

Book Description

This is the Rough Cut version of the printed book.

Need to learn C? Now you can! Forget old-fashioned C programming books that assume you're a computer hardware expert and teach frustrating, old-fashioned coding styles. If you know any language at all, Zed A. Shaw's Learn C the Hard Way can help you get started fast with modern C programming. This book/CD package is called "The Hard Way," but it's really quite easy: it just requires discipline, practice, and persistence. Shaw teaches how to write modern, clean, clear C code through dozens of brilliantly-crafted exercises, most on two pages. You simply read each exercise, type in its sample code precisely (no copy-and-paste!), and make the programs run. As you read, type, fix your mistakes, and watch the results, you learn how C works… what good modern C programs look like… how to read, write, and "see" code… how to spot crucial differences that change or break programs… all the essentials of C logic, I/O, variables, and functions… and the attention to detail that is indispensable to successful C programming. At first, yes, it can be difficult. But it gets easier. And Shaw offers plenty of extra help through 5+ full hours of CD-based teaching video. Nothing important comes without discipline, practice, and persistence. But, with this package, if you bring those qualities, you will master C… and you will reap the very real personal and career rewards that go with C programming expertise!

Table of Contents

  1. About This eBook
  2. Title Page
  3. Copyright Page
  4. Contents
  5. Acknowledgments
  6. This Book Is Not Really about C
    1. The Undefined Behaviorists
    2. C Is a Pretty and Ugly Language
    3. What You Will Learn
    4. How to Read This Book
    5. The Videos
    6. The Core Competencies
      1. Reading and Writing
      2. Attention to Detail
      3. Spotting Differences
      4. Planning and Debugging
  7. Exercise 0. The Setup
    1. Linux
    2. Mac OS X
    3. Windows
    4. Text Editor
      1. Do Not Use an IDE
  8. Exercise 1. Dust Off That Compiler
    1. Breaking It Down
    2. What You Should See
    3. How to Break It
    4. Extra Credit
  9. Exercise 2. Using Makefiles to Build
    1. Using Make
    2. What You Should See
    3. How to Break It
    4. Extra Credit
  10. Exercise 3. Formatted Printing
    1. What You Should See
    2. External Research
    3. How to Break It
    4. Extra Credit
  11. Exercise 4. Using a Debugger
    1. GDB Tricks
    2. GDB Quick Reference
    3. LLDB Quick Reference
  12. Exercise 5. Memorizing C Operators
    1. How to Memorize
    2. The List of Operators
  13. Exercise 6. Memorizing C Syntax
    1. The Keywords
    2. Syntax Structures
    3. A Word of Encouragement
    4. A Word of Warning
  14. Exercise 7. Variables and Types
    1. What You Should See
    2. How to Break It
    3. Extra Credit
  15. Exercise 8. If, Else-If, Else
    1. What You Should See
    2. How to Break It
    3. Extra Credit
  16. Exercise 9. While-Loop and Boolean Expressions
    1. What You Should See
    2. How to Break It
    3. Extra Credit
  17. Exercise 10. Switch Statements
    1. What You Should See
    2. How to Break It
    3. Extra Credit
  18. Exercise 11. Arrays and Strings
    1. What You Should See
    2. How to Break It
    3. Extra Credit
  19. Exercise 12. Sizes and Arrays
    1. What You Should See
    2. How to Break It
    3. Extra Credit
  20. Exercise 13. For-Loops and Arrays of Strings
    1. What You Should See
    2. Understanding Arrays of Strings
    3. How to Break It
    4. Extra Credit
  21. Exercise 14. Writing and Using Functions
    1. What You Should See
    2. How to Break It
    3. Extra Credit
  22. Exercise 15. Pointers, Dreaded Pointers
    1. What You Should See
    2. Explaining Pointers
    3. Practical Pointer Usage
    4. The Pointer Lexicon
    5. Pointers Aren’t Arrays
    6. How to Break It
    7. Extra Credit
  23. Exercise 16. Structs And Pointers to Them
    1. What You Should See
    2. Explaining Structures
    3. How to Break It
    4. Extra Credit
  24. Exercise 17. Heap and Stack Memory Allocation
    1. What You Should See
    2. Heap versus Stack Allocation
    3. How to Break It
    4. Extra Credit
  25. Exercise 18. Pointers to Functions
    1. What You Should See
    2. How to Break It
    3. Extra Credit
  26. Exercise 19. Zed’s Awesome Debug Macros
    1. The C Error-Handling Problem
    2. The Debug Macros
    3. Using dbg.h
    4. What You Should See
    5. How the CPP Expands Macros
    6. Extra Credit
  27. Exercise 20. Advanced Debugging Techniques
    1. Debug Printing versus GDB
    2. A Debugging Strategy
    3. Extra Credit
  28. Exercise 21. Advanced Data Types and Flow Control
    1. Available Data Types
      1. Type Modifiers
      2. Type Qualifiers
      3. Type Conversion
      4. Type Sizes
    2. Available Operators
      1. Math Operators
      2. Data Operators
      3. Logic Operators
      4. Bit Operators
      5. Boolean Operators
      6. Assignment Operators
    3. Available Control Structures
    4. Extra Credit
  29. Exercise 22. The Stack, Scope, and Globals
    1. ex22.h and ex22.c
    2. ex22_main.c
    3. What You Should See
    4. Scope, Stack, and Bugs
    5. How to Break It
    6. Extra Credit
  30. Exercise 23. Meet Duff’s Device
    1. What You Should See
    2. Solving the Puzzle
      1. Why Bother?
    3. Extra Credit
  31. Exercise 24. Input, Output, Files
    1. What You Should See
    2. How to Break It
    3. The I/O Functions
    4. Extra Credit
  32. Exercise 25. Variable Argument Functions
    1. What You Should See
    2. How to Break It
    3. Extra Credit
  33. Exercise 26. Project logfind
    1. The logfind Specification
  34. Exercise 27. Creative and Defensive Programming
    1. The Creative Programmer Mind-Set
    2. The Defensive Programmer Mind-Set
    3. The Eight Defensive Programmer Strategies
    4. Applying the Eight Strategies
      1. Never Trust Input
      2. Prevent Errors
      3. Fail Early and Openly
      4. Document Assumptions
      5. Prevention over Documentation
      6. Automate Everything
      7. Simplify and Clarify
      8. Question Authority
    5. Order Is Not Important
    6. Extra Credit
  35. Exercise 28. Intermediate Makefiles
    1. The Basic Project Structure
    2. Makefile
      1. The Header
      2. The Target Build
      3. The Unit Tests
      4. The Cleaner
      5. The Install
      6. The Checker
    3. What You Should See
    4. Extra Credit
  36. Exercise 29. Libraries and Linking
    1. Dynamically Loading a Shared Library
    2. What You Should See
    3. How to Break It
    4. Extra Credit
  37. Exercise 30. Automated Testing
    1. Wiring Up the Test Framework
    2. Extra Credit
  38. Exercise 31. Common Undefined Behavior
    1. UB 20
      1. Common UBs
  39. Exercise 32. Double Linked Lists
    1. What Are Data Structures
    2. Making the Library
    3. Doubly Linked Lists
      1. Definition
      2. Implementation
    4. Tests
    5. What You Should See
    6. How to Improve It
    7. Extra Credit
  40. Exercise 33. Linked List Algorithms
    1. Bubble and Merge Sorts
    2. The Unit Test
    3. The Implementation
    4. What You Should See
    5. How to Improve It
    6. Extra Credit
  41. Exercise 34. Dynamic Array
    1. Advantages and Disadvantages
    2. How to Improve It
    3. Extra Credit
  42. Exercise 35. Sorting and Searching
    1. Radix Sort and Binary Search
      1. C Unions
      2. The Implementation
      3. RadixMap_find and Binary Search
      4. RadixMap_sort and radix_sort
    2. How to Improve It
    3. Extra Credit
  43. Exercise 36. Safer Strings
    1. Why C Strings Were a Horrible Idea
    2. Using bstrlib
    3. Learning the Library
  44. Exercise 37. Hashmaps
    1. The Unit Test
    2. How to Improve It
    3. Extra Credit
  45. Exercise 38. Hashmap Algorithms
    1. What You Should See
    2. How to Break It
    3. Extra Credit
  46. Exercise 39. String Algorithms
    1. What You Should See
    2. Analyzing the Results
    3. Extra Credit
  47. Exercise 40. Binary Search Trees
    1. How to Improve It
    2. Extra Credit
  48. Exercise 41. Project devpkg
    1. What Is devpkg?
      1. What We Want to Make
      2. The Design
      3. The Apache Portable Runtime
    2. Project Layout
      1. Other Dependencies
    3. The Makefile
    4. The Source Files
      1. The DB Functions
      2. The Shell Functions
      3. The Command Functions
      4. The devpkg Main Function
    5. The Final Challenge
  49. Exercise 42. Stacks and Queues
    1. What You Should See
    2. How to Improve It
    3. Extra Credit
  50. Exercise 43. A Simple Statistics Engine
    1. Rolling Standard Deviation and Mean
    2. Implementation
    3. How to Use It
    4. Extra Credit
  51. Exercise 44. Ring Buffer
    1. The Unit Test
    2. What You Should See
    3. How to Improve It
    4. Extra Credit
  52. Exercise 45. A Simple TCP/IP Client
    1. Augment the Makefile
    2. The netclient Code
    3. What You Should See
    4. How to Break It
    5. Extra Credit
  53. Exercise 46. Ternary Search Tree
    1. Advantages and Disadvantages
    2. How to Improve It
    3. Extra Credit
  54. Exercise 47. A Fast URL Router
    1. What You Should See
    2. How to Improve It
    3. Extra Credit
  55. Exercise 48. A Simple Network Server
    1. The Specification
  56. Exercise 49. A Statistics Server
    1. Specification
  57. Exercise 50. Routing the Statistics
  58. Exercise 51. Storing the Statistics
    1. The Specification
  59. Exercise 52. Hacking and Improving Your Server
  60. Next Steps
  61. Index
  62. Where are the Companion Content Files?
  63. Code Snippets