# Ch 16: Scanning on Demand

> Study guide to Crafting Interpreters chapter 16. A new scanner for clox that produces tokens on demand instead of all at once, pointing into the source…

Updated October 20, 2026.

## Summary {#summary}

Crafting Interpreters chapter 16, Scanning on Demand: A new scanner for clox that produces tokens on demand instead of all at once, pointing into the source string rather than copying lexemes, with a hand-written trie-like switch for keywords. Budget about 4 hours; it is a reading chapter, with no tests to pass.

## Key facts {#key-facts}

- Week 14 of 145 on the cracked.guide path, in [Crafting Interpreters](https://cracked.guide/crafting-interpreters).
- Time budget: about 4 hours.
- Part of the book: clox (bytecode VM, C).
- No test suite for this chapter: you mark it done yourself.
- 3 challenges at the end of the chapter.

## Week 14 goals {#objectives}

By the end of week 14 you can:

- Explain why clox scans tokens on demand instead of all at once.
- Implement tokens as a pointer into the source plus a length.
- Use a trie-shaped switch to recognize keywords.

## This week's steps {#steps}

- Read the chapter: Compare each function with your jlox scanner.
- Write the code in your lox repo: Type it into clox/ (C) yourself as you read, and get it running.
- Do the challenges (1–3): Challenge 1 (string interpolation) is a great design problem; think through how the scanner would need to cooperate with the parser.

## Why it matters {#why}

Zero-copy scanning is how fast real-world parsers work. The keyword switch is a neat trick worth stealing.

## How to approach it {#approach}

Compare each function with your jlox scanner. Note how lexemes are pointers plus lengths, not strings.

## Challenges {#challenges}

3 challenges at the end of the chapter.

Challenge 1 (string interpolation) is a great design problem; think through how the scanner would need to cooperate with the parser. Challenges 2 and 3 are design discussions.

## Common traps {#traps}

- Token lexemes are not null-terminated. Never pass them to printf with %s; use %.*s.
- The source buffer must outlive all tokens.

## Links {#links}

- [Read the chapter: Ch 16: Scanning on Demand](https://craftinginterpreters.com/scanning-on-demand.html)
- [Official repo: book source, both interpreters, test suite](https://github.com/munificent/craftinginterpreters)

---

Source: https://cracked.guide/guide/ci-16
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
