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
cryptonector1 reply#
With C++, going modern-first is a great approach. But C hasn't really been modernized that much, and because Microsoft has been so slow to fully support even just C99, let alone C11 or any GCC/clang extensions, often one has to to stick to a subset of C99 anyways -- exactly as you surmised.

The problem with C is that it's extremely dangerous. Besides learning how to program in C, you'll really want to know how to use memory debuggers such as valgrind and Dr. Memory (https://www.drmemory.org/), as well as AFL for fuzzing, and various GCC and clang sanitizers. You'll really want to let functional programming language experience inform your approach to C API design and implementation.

pjmlp#
Microsoft hasn't been slow, they have been quite clear, C is legacy on Windows, given its security implications, all new code should be C++ and the compiler is called Visual C++ after all.

C support is done to the extent of ISO C++ requirements.
For those that still want to use plain old C on Windows, they have contributed to clang, and it is now available as part of Visual Studio installer.

bane#
The most important difference between a higher level language and C is that you really have to understand a bit about the underlying hardware architecture to do it "right". C is a fairly thin veneer over assembly and it helps to have that mental model in mind when working in it.

It's a very small language with almost no batteries included, built very much around the manipulation of pointers and memory more than just about anything else.
To do C the "right" way means approaching problems from that perspective and building mountains out of toothpicks while thinking about how the billions of toothpicks need to interact with one another so they don't crash/catch fire/etc.

WalterBright2 replies#
I'd just type in the examples from K+R, try them out, and modify them to do new things.
elliotec#
Do this but get the answer book too, because it's easy to get stuck in here.
wiredfool2 replies#
K+R is pretty ancient.
WalterBright#
So is C :-)
saagarjha#
That doesn't make it worthless; it's still probably the best book to learn C today if supplemented correctly.
vocram1 reply#
Becoming proficient in a language usually involves: - writing code with it long enough, - collaborate in a project based on it, - reading code other people wrote, - keep yourself up to date by following the language evolution, - go more in depth in language internals, compilers, etc...

I wrote C for 10+ years (mostly bare metal FW), yet I am still amazed of how little I know about it. Recently for example I learnt of all the things the dynamic linker does in Linux, how symbols memory addresses are resolved at runtime using PLT, ....
The good point about C is that it can be used for very different kind of projects, so the choices are a lot.

saagarjha2 replies#
(Please don't write code like this anymore)
stiray#
Sure, duff device is obsolete due to cpu zero overhead looping but it is still nice to see, mind boggling piece of code. :)
mrr54#
(Please do write code like this anymore.)

Load more comments