You are previewing Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C).
O'Reilly logo
Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C)

Book Description

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.

    To access the companion files for this title:

    1. Go to http://www.informit.com.

    2. Create an account or log in if you have an existing one.

    3. Access the "Account" screen and register your book.

    On the book's homepage the tab(s) labeled "Downloads" (and/or "Extras") will have the files you are looking for.

    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