Earlier quoted context omitted.
> It's always been amazing to me how many different projects I've worked on (not that I've been in professional C for about 7 years now)) that include their own painstaking recreation of this file. How many of them started before stdint.h existed? AFAIK, it's a somewhat recent addition to the C language, and IIRC, for a long time even after it became part of the C standard, some popular C compilers still didn't have…
inttypes.h was added to C99. A quarter of a century ago.
My personal C coding style as of late 2023
311–320 of 466 posts
Re: My personal C coding style as of late 2023
#312Earlier quoted context omitted.
(self-reply) One more thing. > I could use _Bool, but I’d rather stick to a natural word size and stay away from its weird semantics. This is even more subjective, but personally I like _Bool's semantics. They mean that if an expression works in an `if` statement: if (flags & FLAG_ALLOCATED) then you can extract that same expression into a boolean variable: _Bool need_free = flags & FLAG_ALLOCATED; The issue is that…
I always just use !! for this case.
Re: My personal C coding style as of late 2023
#313http://edit.rupy.se/?host=move.rupy.se&path=/file/game.cpp&s...
It's messy and in many ways ugly but it compiles and runs for an eternity.
Re: My personal C coding style as of late 2023
#314Earlier quoted context omitted.
Thanks for bringing this up. There is no reason for modern languages that have proper constants (not preprocessor macros which happen to sometimes be used for them) to use this tedious style. Constants are so innocent and useful. Why indirectly discourage their use by making their usage an eye-bleed?
What alternative would you suggest?
static constexpr int kConstantName = 42;Re: My personal C coding style as of late 2023
#315Great article. One thing for me is that I think we named our variables wrong. We should be specifying the number of bytes, not bits. I use U1, U2, U4, U8, I1, I2, etc Also S for "slot" aka unsigned pointer sized integer (usize_t) Another big point is formatting code to line-up instead of with an autoformatter. When you are doing something which is almost the same but slightly different it helps readability considerab…
Most of the time when I use fixed-width int types I’m trying to create guarantees for bitwise operators. From my perspective I feel like it therefore makes the most sense to name types on a per-bit level.
Re: My personal C coding style as of late 2023
#316Earlier quoted context omitted.
I mean, being a bit glib here, but a lot of programming is dealing with someone else's type system. Moreover, for those of us who write C fairly often, the mnemonics here are familiar. Actually, as custom type systems go, this one is pretty elegant. Reminds me of Rust.
Yeah but not for basic types. Also, most code mingles sooner or later with other code. Than this is just ugly.
Re: My personal C coding style as of late 2023
#317Earlier quoted context omitted.
For example, it would be illegal to do the following: > #define int long Because you're replacing the int keyword with something else. The standard says: > 17.6.4.3.1 [macro.names] paragraph 2: A translation unit shall not #define or #undef names lexically identical to keywords, to the identifiers listed in Table 3, or to the attribute-tokens described in 7.6.
That's C++. But I couldn't find the same restriction in C. In fact it seems that C allows it as long as you don't include any of the standard C header. > 7.1.2 "Standard headers" §5 [...] The program shall not have any macros with names lexically identical to keywords currently defined prior to the inclusion of the header or when any macro defined in the header is expanded.
In C standard, it is not allowed.
> C11, § 6.4.1 Keywords
> [...]
> The above tokens (case sensitive) are reserved (in translation phases 7 and 8) for use as keywords, and shall not be used otherwise.
https://stackoverflow.com/questions/12286691/keywords-redefi...
Re: My personal C coding style as of late 2023
#318> While I still prefer ALL_CAPS for constants, I’ve adopted lowercase for function-like macros because it’s nicer to read. "ALL_CAPS" in C was not for constants, but for preprocessor macros. It's shouting in all-caps, because it means "Look out! There's a cpp macro expansion here!" Related, please stop using "ALL_CAPS" for constants in other languages. Not only does shouting constants as the most prominent syntax in…
It's the reason Rust makes us use an exclamation mark with macro calls: beware! magic! here! I like this as a convention, but not necessarily as a grammar rule. Printing values is common enough that it shouldn't require shouting for constant attention, simple code shouldn't trigger sensory overload.
Re: My personal C coding style as of late 2023
#319> typedef float f32; > typedef double f64; Assuming float is 32 bits and double is 64 bits sounds like a foot-gun. OpenCV defines a float16_t [0], CUDA implements half-precision floats [1], micro-controllers implement whatever they want. C++23 introduces fixed width floating-point types [2], but not aware of any way to enforce this in C. What I would suggest it to have a macro to check data is not lost at compile tim…
gcc has _Float types typedef _Float32 f32; typedef _Float64 f64; https://gcc.gnu.org/onlinedocs/gcc/Floating-Types.html
Re: My personal C coding style as of late 2023
#320Hopefully I never need to review code written with these definitions. It's an awful idea to do this. The first section shows why some languages have no 'typedef': introducing another layer of aliases is just not a good idea. It's confusing, it changes the appearance to basically a new language. Just use the standard names, instead of redefining your language, like everyone else. This style is almost as bad as '#defin…
Their `size` type is signed. It's `ptrdiff_t`, not `size_t`.