
C Traps and Pitfalls
by Andrew Koenig · first published 1989
79% recommend17% neutral4% critical
Why HN recommends it
Catalogs C's dark corners and anti-patterns, stressing clarity and avoiding misreadings by humans or compilers. Suits programmers who already know K&R and want to think like a C programmer.
Summarised by Claude from recommending comments
Even experienced C programmers can get themselves into trouble if they're not certain of what they're doing. Each of the book's many examples has trapped a professional programmer. C Traps and Pitfalls aims to encourage defensive programming by showing how other people, even experienced professionals, have gotten themselves into trouble. These mistakes are generally easy to avoid once seen and understood, so the emphasis is on specific examples rather than generalities. Covers lexical, syntactic, and semantic pitfalls, linking and library problems, preprocessor, and portability pitfalls.
Affiliate links. As an Amazon Associate I earn from qualifying purchases.







…a bit dated. As the parent said, you'll appreciate C only if you learn about machine architecture and Unix. Diving into a large codebase will get you acquainted with "modern" C. I've found the book C Traps and Pitfalls to be the book to get you to think like a C programmer. The C Puzzle Book is pretty good for exercises, and Expert C Programming does a good job exposing the ugly side of C.
on “Ask HN: Learning C” ↗…Everything from there up seems to be self-taught, either from the internet, code examples, or working it out from first principles. There are a lot more risks in C than more recent languages. Koenig's book "C Traps And Pitfalls" https://www.amazon.co.uk/C-Traps-Pitfalls-Andrew-Koenig/dp/0... was extremely useful to me when I was learning the language (available at my local library!). I don't know if there's a more modern version…
on “Teaching C (2016)” ↗…clean, like C), though Python and Lisps/Schemes that compile to C work well too. (I can't vouch for Ruby here, since I already know Python and Lua and haven't bothered with it.) See also: Andrew Koenig's "C Traps and Pitfalls" (http://www.literateprogramming.com/ctraps.pdf) and the book, which expands on the paper.
on “C Craft” ↗For far more of same, there's Koenig's _C Traps and Pitfalls_. Heck, the article (http://www.literateprogramming.com/ctraps.pdf) that grew into the book is even free online. Also, for anybody doing modern C work, I strongly recommend both Hanson's _C Interfaces…
on “C Language Quirks” ↗…programming. Maybe other people have better recommendations than that..? Separate to that "The C Puzzle Book" is superb when you're learning to really thoroughly understand pointers, arrays and so on. Koenig's "C Traps and Pitfalls" isn't bad for some of the dark corners.
on “What they don't teach you about sockets” ↗…from H&S. From this list (can't say how current they are), http://www.c-faq.com/resources/sd19.html I liked: - K&R and H&S (if you have to choose, H&S) - The Standard C Library, P.J.Plauger. - C Traps and Pitfalls, Andrew Koenig.
on “Ask HN: I use and love Python. What's the best book to learn C ?” ↗…may be using the word 'modern' a bit liberally here, but these are all more recent than K&R at least. 'C A Reference Manual' I think is what a lot of people really want out of a C book and 'Expert C'/'C Traps and Pitfalls' both help with all of the less intuitive parts of the language that you don't really get out of K&R. C A Reference Manual http://www.amazon.com/Reference-Manual-Samuel-P-Harbison/dp/... Expert C http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp...…
on “C is 38 years old. K&R V2 is 22 years old. What's the best modern C book?” ↗Also recommend reading C Traps and Pitfalls. http://www.literateprogramming.com/ctraps.pdf
on “Ways to get screwed by C” ↗For a beginner's introduction to C++, I really liked Koenig and Moo's Accelerated C++. It's from 2000, I don't know if there's a newer edition. Speaking of Koenig, I also like his C Traps and Pitfalls (1989). Sedgewick's Algorithms is excellent. My copy is 30 years old, but I know there are newer editions. I believe it's multi-volume now. I liked two by Richard Stevens: Unix Network Programming…
on “Recommended Reading List for Developers [pdf]” ↗…book is also not a tutorial on programming, either. Thus if you're a n00b, you will not learn programming from K&R2 and you will not learn C properly. A combination of K&R2 together with Andrew Koenig's C Traps and Pitfalls might be better. If you have those two books, plus the Harbison and Steele reference manual and a PDF of a draft standard, you're in very good shape as far as being well-informed. You still don't have…
on “Ask HN: What are some good resources for learning C?” ↗When you're done with The C Programming Language, read Expert C Programming, C Traps and Pitfalls, and The C Puzzle Book. Then tell me how simple C is.
on “A critique of "How to C in 2016"” ↗Seconded. I also recommend C Traps and Pitfalls and The C Puzzle Book to improve your understanding of this subtly complex language.
on “Ask HN: Improve C language skills after K&R?” ↗Very old, but "C traps and pitfalls": http://www.goodreads.com/book/show/706807.C_Traps_and_Pitfal... It's essentially a list of anti-patterns to avoid. But crucial to it is the idea of clarity and avoiding misinterpretation by either…
on “Ask HN: What books had the greatest effect on how you structure your code?” ↗Programming: C Traps And Pitfalls. Most programming books are focused on the happy path; in reality we spend most of our time in the unhappy one, trying to fix or debug something. That book made it a lot more hospitable to be in that…
on “Ask HN: What books changed the way you think about almost everything?” ↗…along the lines of "fail to see the forest for the trees" I think I'm going to need an example to understand this? Having said that, one of the most informative programming books I've ever read was C Traps And Pitfalls. Flags common easily-made errors and explains them, which in turn fixes misconceptions about the language. I feel most languages could do with one.
on “Ask HN: Examples of bad open-source code to learn what to avoid?” ↗