My C23 Conventions

32 points by abnercoimbre 6 hours ago on lobsters | 5 comments

tomsmeding | 2 hours ago

Years ago I was severely distrustful of any kind of zero initialisation in C, mostly since I could never remember which magical incantation ({}, {0}, were there edge cases?) did the trick, and would write an explicit memset after the variable declaration. Then I got tired of that, found the correct syntax, and wrote {0} instead.

Now I wonder if the problem is C, not me.

[OP] abnercoimbre | 2 hours ago

It really is a mistake that {0} doesn't necessarily zero-initialize the union members inside a struct (C standard only requires that compilers zero-out the first member. So please make sure your first member is larger than the rest in the union layout.)

It's even more wild that gcc would zero the whole thing and clang would not, then they reverse-uno'd last year, so now they're doing opposite behaviors? What the hell is going on?

C23 fixes the disaster with { } so I'm using that everywhere now. It works great. I gotta say though it's quite odd how {0} is the syntax with a zero in it yet it won't reliably do the right thing. It seems the Standards Committee would rather avoid redefining the meaning of things… but c'mon they did it with auto :D

nytpu | 2 hours ago

I use C23 religiously almost exclusively because of constexpr giving truely compile-time constant expressions lol, was very desperately needed for a long while. I always despised having to abuse anonymous enums to get true-constant integers (still better than preprocessor since they're actually a language feature and as such show up in debuggers and such) and then use the preprocessor for any other kind of constant. (const objects are just "immutable", not actually a compile-time constant; so you can't necessarily use them for things like static array lengths)

confusomu | 30 minutes ago

Indeed, C23 is very nice with its constexpr constant expressions. With these and static_assert, you can do a lot of compile-time checks, such as this, with some preprocessor and this peripheral setup. It makes C a nicer language to use, and these compile-time techniques avoid having to reach for C++ in simpler cases.

alterae | 20 minutes ago

i do appreciate that these all seem to boil down to just "using c23"