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
chucksmash#
You might also take a look at this Minecraft clone in C[1]. Uses sqlite to persist state so you can see how you might interact with a database as well. Quite modern and readable in that most functions outside main.c are <20 lines long. It's also cross platform and should work on Windows, Mac and Linux.

[1]: https://github.com/fogleman/Craft

jstimpfle1 reply#
omg no. Stop obsessing about this dragon. I'm not sure I've ever met it in my life (you know, other than segfaults from NULL dereferences which are UB too).
saagarjha1 reply#
How much C have you written, and how much has gone through a high-performance optimizing compiler?
jstimpfle2 replies#
I don't know. Quite a bit - maybe written 300K lines and deleted > 200K lines. Most of it was compiled by gcc -O2 and/or MSVC. I've never relied on vectorization or used a lot of intrinsics, though.

Continue this thread — 2 replies

flohofwoe#
The Visual Studio C compiler (not the C++ compiler) supports "almost all" of C99, I would definitely recommend skipping C89, and use at least the new initialization features from C99 because they can reduce bugs caused by uninitialized data. Also, C++ compilers support a subset C99 that's somewhere around "C95" and is already a much friendlier version compared to C89.

TBH, I think the best way to learn C is to tinker a bit with assembly first (it doesn't have to be a modern assembly dialect, just write a few 6502 or Z80 assembly programs in a home computer emulator). Coming from "below" instead of "above" C's abstraction level helps understanding the design rationale behind C, and the trade-offs between flexibility and unsafe memory handling.

dandigangi#
I learned a few basic levels of C using my subscription to pluralsight.com.

But... I quickly switched to Rust as I see it being the C of the future as it continues to develop. That's just me though. (I have similar opinions towards things like Kotlin or Go.)
The books I am reading in the comments below are great resources.

Load more comments