Exercise 23. Meet Duff’s Device

This exercise is a brain teaser where I introduce you to one of the most famous hacks in C called Duff’s device, named after Tom Duff, its inventor. This little slice of awesome (evil?) has nearly everything you’ve been learning wrapped in one tiny, little package. Figuring out how it works is also a good, fun puzzle.

Warning!

Part of the fun of C is that you can come up with crazy hacks like this, but this is also what makes C annoying to use. It’s good to learn about these tricks because it gives you a deeper understanding of the language and your computer. But you should never use this. Always strive for easy-to-read code.

Discovered by Tom Duff, Duff’s device is a trick with the C compiler that actually shouldn’t ...

Get Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.