good C code

Mar 28, 2022

What makes good C code?

Here's some that I consider good:

Note the focus on clarity, simplicity, and on cleaning everything up.

One thing most people overlook: Experienced C developers often don't use it as a low-level language. Rather, they use libraries which extend the language with higher level constructs.

C doesn't have linked lists as a native data type. But include the right header and you'll have macros for declaring a linked list and inserting/removing/traversing. Same goes for "elastic" (automatically re-allocating as needed) arrays. Not in C, but with the right header...

Which is in response to a Dan Luu thread:

The security counter-argument is:

In my neck of the woods, people get flak for using C/C++ because of the security externalities, not because of some scorn for the effectiveness of the tool. There is no proof that anyone is effective at writing secure code in C/C++. It's not the wrong brush, it's lead paint. - Filippo Valsorda

↑ up