Lurker News
I have 15+ years of experience in many languages: javascript, python, go, etc.

I can write C, but I'm not confident I'm doing it the right way. There are now decades of forums and blogs I can search through, but I'd love to have a single, clean, clear source of truth for recommendations on how to write C code.
I've started reading "Modern C" by Jens Gustedt. I'm not sure if this is the closest to a modern source of truth.
Note, I'd like my C code to work on Windows, which possibly restricts the "right way" to features provided by C89. But again, I'm not sure, the internet is super clear on this.
Thanks for the tips!

Comments (199) 63 threads

Earlier comments
begriffs#
It's hard to beat this list of quality books:

http://iso-9899.info/wiki/Books#Learning_C
Read some and do the exercises. You'll learn to write portable code to a standard rather than stuff that "accidentally works." There's a lot of crap C code in the world, full of GNUisms, overzealous macros, header-only nonsense abusing translation units, and copypasta. Don't pick up bad habits from that stuff.

carapace#
Learn (a little bit of) assembly first. It will make more sense IMO if you came at it from below rather than above, so to speak.
Areading3142 replies#
I would recommend "Learn C the Hard Way" by Zed Shaw to get you started for something practical, because it goes over how C is written in the real world. Of course, situations will vary. The original book about C by K&R is an excellent book too.
Paperweight3 replies#
Zed makes it clear in that book that he hates C and doesn't use it anymore due to its undefined behaviour.
Areading314#
Smart man!
saagarjha#
Zed isn't actually very good at understanding undefined behavior and has demonstrated this loudly and repeatedly. I would steer clear of his book, personally.
Jugurtha#
> Zed makes it clear in that book that he hates C and doesn't use it anymore due to its undefined behaviour.

Really? Which part in the book specifically if you can cite the passage.

mrr54#
Learn C the Hard Way is considered by the community to be, not exaggerating, one of the worst C books ever written.
keithnz#
There is no clear source of truth for C. Depending on what you want to do there is quite different approaches. C for embedded systems tends to look quite different from C for systems programming. For instance MISRA is a C coding standard for embedded devices in Motor Vehicles (but many of its ideas are ideas from writing robust embedded systems), it says don't use things like dynamic memory. You'd never use that if you are writing software for linux/windows. So learning C "properly" would be about learning the different approaches people use in C to write robust code and what tradeoffs they are making. Understanding the C standard and the variations. Understanding bare metal programming. Understanding the effect of different underlying processors and hardware architectures. Understanding systems programming and how to consume APIs and how to create reusable binary modules. Understanding the compiler/build system the intermediate files / code generation / memory layout. Read a variety of books ( including K&R ) and study code ( Luas implementation is some very nicely written C )

Load more comments