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
tmotwu#
Like anything else, set yourself a goal for a project. What kinds? C today is typically used for developing embedded systems, kernel internals, or device drivers.

Depending on how deep you want to go. The most in depth way to understand C is to learn assembly on an ISA first.

pyuser5831 reply#
If you’re an experienced programmer, with some understanding of C, I recommend: “Expert C Programming: Deep Secrets” by Peter van der Linden.

It’s one of the best books in the world. About anything. Ever.

jjgreen#
I was going to post the same, now I don't have to, fabulous book.
jussij2 replies#
> Note, I'd like my C code to work on Windows, which possibly restricts the "right way" to features provided by C89

That might be the case if you plan to use the Microsoft C compiler as they have publicly said the don't have plans to update their C compiler.
However that is not your only option, since clang and several GCC ports will also work on Windows.

giancarlostoro2 replies#
Are you not able to use their C++ compiler to get modern features? My understanding is that C++ is a superset and that a C++ compiler is also usually a C compiler. Is this view incorrect for modern implementations?
jstimpfle1 reply#
It's not strictly a superset, to the point that there are good reasons to stay with a C compiler other than to simply stay sane. I make a lot of use of designated initializers for laying out static const arrays. And I actually don't like the way that structs in C++ can be referred to both with and without the struct tag. I also don't like how void-pointers are not compatible with other pointers without an explicit cast. Or that plain integers cannot be assigned to enum types. Some of this is just my personal preference (or Stockholm Syndrome), but the fact is that most C code will fail to compile as C++ without some changes.

MSVC is probably pretty close to C99 these days. Designated initializers are C99, for example.

Leherenn1 reply#
You can assign integers to enum in C++, just like in C, but not to enum class. Use the latter when you need type safety, the former when you need to easily convert integer <-> enum, which apart from (de)serialisation is rare in my experience. And even in those cases, I personally prefer the explicit casts.

As far as I know, MSVC just implements the C subset of C++.

Continue this thread — 1 reply

jki2752 replies#
C++ is not a strict superset of C anymore. You can't compile all C in a C++ compiler.
mrr541 reply#
To use a trivial example:

    int free;

Continue this thread — 1 reply

leetcrew1 reply#
has c++ ever been a strict superset of c?

afaik,

  int* arr = malloc(sizeof(int));
has never worked in c++.

Continue this thread — 1 reply

asveikau1 reply#
Microsoft has started to implement more of C99 over the years. I think it was 2015 where they implemented mixed declaration and code. I noticed they put stdint and stdbool finally in 2010 or so.

Still pretty incomplete last I checked but portability is better than it was.

pjmlp1 reply#
Those features are based on ISO C++ requirements, regarding the adoption of underlying C features and respective standard library.
asveikau2 replies#
Mixed declaration and code is very old in C++, and Microsoft has always supported it there. It wasn't until vs2015 that they let you do it in a .c file.

Continue this thread — 2 replies

juped#
This is actually pretty hard. I recommend reading C codebases, but it can be hard to know which ones are good. Code to C99 for now; keep an eye on C11 etc. Have a personal library of C implementations of things. Don't worry about being on Windows, although if you're doing Windows development C#/.NET is the lowest friction path.

I'm writing a book on C. I don't think it will ever be finished or see publication. I recommend writing a book yourself - it's a great way to learn, and maybe you're a better writer than me and will publish it.

Load more comments