Did you know that in C, character literal is an int, not a char like in C++?
I was somewhat astonished

@crusom that’s the beauty (and curse) of C - it treats literally everything as binary numeric data, with “types” only instructing the rest of the code and the compiler how to deal with it. A char is literally just a byte-sized int IIRC. Hell, headers defining stuff like uint8 basically do typedef uint8 unsigned char xD The thing that illustrates that approach best is the union type. You declare a structure where every member uses the same memory - it’s only the size of its biggest member. So you can at all times, with the compiler’s blessing, just look at the raw binary data of one type by requesting it to be read as another type.

It’s as fascinating as it’s barely useful and usually a bad practice. This kind of binary voodoo is what makes C a pain to work with for people used to the comforts of high-level languages.

@Amikke union was useful to me when I implemented the chip8 interpreter. (In c++)
Idk if it's a bad practice, it looks clear and works correctly


@crusom idk either, there are legit use cases where they are the right tool for the job.

This discussion reminded me of another C weirdness - the int type itself. C was designed so that the int was the size of a processor word, making its size architecture-dependent. They warned about that in the books, all of the cool libraries used type aliases to explicitly declare (u)int32 wherever needed, yet when 64-bit processors arrived… int being 32-bit was so engrained in the ecosystem that they gave up. And so despite what you may have learned, int is 32-bit even on a 64-bit architecture, because the potential problems of changing it mounted up too high.

I was quite surprised when I was playing with Julia’s C interface and discovered that Cint is an alias of Int32, prompting me to go down the rabbit hole of “why?” and “but wait, I thought…”. Julia, in contrast, avoids this problem by declaring Int32 and Int64 types and aliasing Int to whatever is native to the architecture.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves. A STEM-oriented instance.

An inclusive free speech instance.
All cultures and opinions welcome.
Explicit hate speech and harassment strictly forbidden.
We federate with all servers: we don't block any servers.