Double-entry bookkeeping has kept the world's books for more than five centuries. This book explains it from first principles, and builds it in C as you read.
The book is a literate program: the code you read on the page is the code that gets compiled. Chapter by chapter it builds `libledger`, a small C library that keeps double-entry books in a single SQLite file. On top of the library it builds the `ledger` command and an MCP server through which an AI assistant can keep the books, but only with your approval.
You need to know some C. You don't need to know any accounting.
You will follow Alex, a freelance graphic designer, through her first week of books: invoicing a client, paying rent, fixing a mistake, getting paid, closing the period. Later you will live one of her days twice, once by hand and once by talking to an assistant, and see the balances come out the same.
Along the way you will learn:
- Accounting: debits and credits, the accounting equation, a US-style chart of accounts (49 accounts), accrual vs. cash, the trial balance and closing entries.
- Data modeling: an 8-table SQLite schema, an account tree derived from dotted codes, and cached balances that always agree with their history.
- Money done right: amounts are 64-bit integers in the smallest unit, with overflow checks. Floating point is never used for money. Every currency balances on its own, and values are converted to USD (mark to market) only when the books are read.
- Correctness: every entry is validated and written inside one `BEGIN IMMEDIATE` transaction. Negative balances are rejected, and four consistency checks catch books damaged by hand.
- Tooling: a single-binary CLI, JSON input and output, and an MCP (Model Context Protocol) server with a ready-to-install skill for the assistant.
The stories in the book are executable chapters. The build extracts their shell sessions, runs them, and compares the output character by character with what the book prints, so the text cannot drift away from the program.
About 3,000 lines of C, with no dependencies beyond SQLite and cJSON. Each technical term, accounting or programming, is defined in a glossary at the end of the book.
A book that tests itself
One distinctive feature of this project is that the book and the software share the same source.
Using noweb, the implementation is extracted from the manuscript and compiled into working programs. Selected chapters go further: their command-line sessions are executed automatically, and their output is compared against the results printed in the book.
When the implementation changes, the documentation can be verified against it.
Who is this book for?
This book is intended for programmers who want to understand how financial software works, developers interested in literate programming, and engineers who appreciate small, understandable systems built from first principles.
Readers should have some familiarity with C. No previous accounting knowledge is required.
The result is both an introduction to double-entry bookkeeping and a detailed exploration of how to turn accounting principles into executable, verifiable software.
All the code is on GitHub: https://github.com/libledger/libledger