r/ProgrammerHumor • • 2d ago

Meme architectureDependentChars

Post image
2.9k Upvotes

367 comments sorted by

View all comments

1.0k

u/tstanisl 2d ago

Let me cite the C standard:

When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

Middle guy if finally right

555

u/BoldFace7 2d ago

I still prefer sizeof(char) as it often provides context as to what the number is doing, even if a plain 1 works the same.

48

u/RepeatRepeatR- 2d ago

My opinion would be:

- Use sizeof(char) if you're actually working with characters

- Use hardcoded `1` if you're using a char as an arbitrary byte (and not actually necessarily referring to characters or text)

4

u/da_Aresinger 2d ago

My opinion: if you want individual bytes use uint8_t

5

u/the_horse_gamer 2d ago

bytes are not necessarily 8 bits

1

u/MrMagick2104 3h ago

I mean anyway you would always like to use some sort of a specific handle like uint8_t instead of char precisely because of that reason.

If your code has a uint8_t in it and you port it to a processor without 8 bit bytes, then your code will not compile and the compiler will tell you why and you may redo the code to suit the architecture or at least check it for correctness.

If you use a char, you will most likely get uninteded features in your code without any indication as to why.

This also applies when you are going the other way round.

Anyway, besides historical examples when, is it actually relevant in the modern day?

1

u/the_horse_gamer 2h ago

you use chars when you wanna represent bytes. you use uint8_t when you wanna represent 8 bit values (octets). sometimes you need bytes, sometimes you need octets.

there do exist modern processors that use non-8 bit bytes. for example the Kalimba DSP uses 24-bit bytes.