Leanpub Header

Skip to main content

C From Scratch

Prove it works before you write it.

Learn C by proving your code correct before you write it. The same methodology behind certified aerospace and medical systems - now applied to learning C from the ground up.

Minimum price

$9.99

$19.99

You pay

$19.99

Author earns

$15.99
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
WEB
About

About

About the Book

One line of code crashed the Ariane 5. One line killed patients with the Therac-25. One line cost Knight Capital $440 million in 45 minutes.

Different domains. Same cause: software that failed silently until it was too late.

This book teaches you to write C that fails loudly, early, and for a reason - or not at all.

The approach comes from aerospace and medical device certification, where "it works on my machine" isn't acceptable - and undefined behaviour isn't a learning experience, it's a liability.

You'll learn to:

  • Define correctness before a single line is written
  • Design data structures that make invalid states unrepresentable
  • Write code as a literal transcription of proven contracts - not guesswork

Test behaviour, not hope

What You'll Build

11 chapters take you from fixed-width integers to a complete, provable state machine - each concept grounded in real-world failures and how to prevent them.

The progression:

1. The Philosophy - why proving first changes everything

2. Fixed-Width Integers - why `int` is a trap

3. Booleans and Decision Logic - contracts that catch bugs at compile time

4. Arrays - bounds you can prove

5. Strings - buffer safety without the mystery

6. Structures - data design that prevents invalid states

7. Pointers - ownership made explicit

8. Functions - contracts as code

9. State Machines - complexity you can reason about

10. Capstone: Tic-Tac-Toe - prove correctness before writing game logic

11. Path to Production - from learning to shipping

Plus four appendices: Development Setup, Contracts Quick Reference, Fixed-Width Types Reference, and Common Undefined Behaviours.

Who This Is For

  • Self-taught developers wanting rigorous foundations
  • Engineers moving into embedded or safety-critical domains
  • Anyone tired of debugging code that "should" work
  • Students who want to understand why not just how

No prior C experience required. Just a willingness to think before you type.

What Makes This Different

Most C books teach syntax. This one teaches thinking.

You'll spend less time memorising rules - and more time eliminating entire classes of bugs before they exist.

The methodology - MATH → STRUCT → CODE → TEST - is how certified systems are actually built. Before writing a single line, you define the mathematical properties your code must satisfy. Then design structures that enforce them. Then implement. Then verify against the original proof.

Not because you'll build a pacemaker, but because the discipline makes you a better programmer.

By the end of this book, you won't just write C - you'll reason about it.

Author

About the Author

William Murray

William Murray is a Regenerative Systems Architect with 30 years of UNIX infrastructure experience. He created the c-from-scratch open source course and the certifiable-* ecosystem for safety-critical ML systems (GPL-licensed). He developed the Murray Deterministic Computing Platform (MDCP), a complete technology stack for certifiable autonomous systems. Founder of SpeyTech,com and Visiting Scholar at Heriot-Watt University, he lives in the Scottish Highlands.

Contents

Table of Contents

The Philosophy

  1. Why This Matters
  2. The Core Insight
  3. MATH: Define Correctness First
  4. STRUCT: Make Invalid States Unrepresentable
  5. CODE: Implementation as Translation
  6. TEST: Verify the Contract
  7. What This Buys You
  8. This Isn’t Just Theory
  9. Your Development Environment
  10. The Path Forward
  11. Exercises
  12. Summary

Fixed-Width Integers

  1. Why int Isn’t Good Enough
  2. The Mathematics of Bounded Integers
  3. Fixed-Width Types in C
  4. Choosing the Right Type
  5. Overflow: What Happens at the Edges
  6. Safe Arithmetic Operations
  7. Type Conversions: Where Things Go Wrong
  8. Integer Literals
  9. Printing Fixed-Width Integers
  10. Common Pitfalls
  11. Testing Integer Operations
  12. Exercises
  13. Summary

Booleans and Decision Logic

  1. The Mathematics of Truth
  2. Booleans in C
  3. Comparison Operators
  4. Logical Operators
  5. Decision Structures
  6. The switch Statement
  7. Predicates as Functions
  8. Guard Clauses
  9. Defensive Programming
  10. The Therac-25 Lesson
  11. Testing Decision Logic
  12. Exercises
  13. Summary

Arrays

  1. The Mathematics of Bounded Sequences
  2. Arrays in C
  3. Safe Array Access
  4. The Heartbleed Pattern
  5. Array Initialisation
  6. Passing Arrays to Functions
  7. The sizeof Trap
  8. Bounded Buffers
  9. Off-By-One Errors
  10. Multi-Dimensional Arrays
  11. Array Copying
  12. Searching Arrays
  13. Testing Arrays
  14. Exercises
  15. Summary

Strings

  1. The Mathematics of Text
  2. C Strings
  3. The Danger of Unbounded Operations
  4. Safe String Operations
  5. Bounded String Buffers
  6. String Comparison
  7. Character Classification
  8. Parsing Numbers from Strings
  9. Format Strings: Another Attack Vector
  10. Testing Strings
  11. Exercises
  12. Summary

Structures

  1. The Mathematics of Composite Data
  2. Structures in C
  3. Initialisation
  4. Structures with Invariants
  5. Nested Structures
  6. Structures and Functions
  7. Memory Layout
  8. Opaque Structures
  9. The Mars Pathfinder Lesson
  10. Testing Structures
  11. Exercises
  12. Summary

Pointers

  1. The Mathematics of Indirection
  2. Pointers in C
  3. Pointer Validity
  4. Safe Pointer Patterns
  5. Pointers and Arrays
  6. Pointers to Pointers
  7. Function Pointers
  8. Memory Allocation
  9. The Futex Vulnerability
  10. Testing Pointers
  11. Exercises
  12. Summary

Functions

  1. The Mathematics of Functions
  2. Function Contracts
  3. Implementing Contracts in C
  4. The Knight Capital Failure
  5. Function Design Principles
  6. Pure Functions
  7. Functions with Side Effects
  8. Recursion and Its Limits
  9. Testing Functions Against Contracts
  10. Exercises
  11. Summary

State Machines

  1. The Mathematics of State
  2. States in C
  3. Events
  4. The Transition Function
  5. Actions
  6. Completeness and Safety
  7. The Therac-25 State Machine
  8. Hierarchical State Machines
  9. Testing State Machines
  10. Exercises
  11. Summary

Capstone: Tic-Tac-Toe

  1. MATH: Defining the Game
  2. STRUCT: Data Representation
  3. CODE: Implementation
  4. TEST: Verification
  5. Putting It Together
  6. What We Demonstrated
  7. Exercises
  8. Summary

Path to Production

  1. The Standards Landscape
  2. From Learning to Certification
  3. Tools of the Trade
  4. Building Your Career
  5. Resources for Further Learning
  6. The Mindset
  7. Where You Stand
  8. Final Exercises
  9. Summary

Appendix A: Development Setup

  1. Compiler Installation
  2. Compiler Flags
  3. Static Analysis Tools
  4. Dynamic Analysis Tools
  5. Test Framework Setup
  6. Makefile Template
  7. Continuous Integration
  8. Directory Structure
  9. Summary

Appendix B: Contracts Quick Reference

  1. Contract Documentation Template
  2. Precondition Patterns
  3. Postcondition Patterns
  4. Invariant Patterns
  5. Assertion Macros
  6. Contract Patterns by Function Type
  7. Testing Contracts
  8. Recovery Strategies by Failure Type
  9. Summary

Appendix C: Further Reading

  1. Books
  2. Papers
  3. Standards
  4. Online Resources
  5. Open Source Safety-Critical Projects
  6. Author’s Resources
  7. Summary

Appendix D: Common Undefined Behaviours

  1. Integer Undefined Behaviours
  2. Pointer Undefined Behaviours
  3. Array Undefined Behaviours
  4. String Undefined Behaviours
  5. Type Undefined Behaviours
  6. Sequence Point Undefined Behaviours
  7. Control Flow Undefined Behaviours
  8. Concurrency Undefined Behaviours
  9. Quick Reference Table
  10. The Golden Rule

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub