O'Reilly logo

Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming by Zed A. Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required