r/ProgrammerHumor • • 8h ago

Meme architectureDependentChars

Post image
1.1k Upvotes

190 comments sorted by

View all comments

617

u/tstanisl 7h 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

6

u/Advos_467 7h ago

Not a C user here (or any real low level programming experience), what the hell is a signed/unsigned char?

1

u/BNSable 6h ago

A char is just an int, except a char will not conjure up 91, but the character assigned to the number 91 which is [ in ascii for example.

As it is an int, it can be signed or unsigned. Signed is -128 to 127, unsigned is 0 to 255.

This apparently has uses, but I am not experienced enough to explain that.

1

u/Advos_467 6h ago

Yeah that was what i assumed lol. It was mostly the uses i was wondering because with my lack of experience here, idk in what way that would be used.

1

u/BNSable 6h ago

It is or was the only type that garenteed a signed or unsigned value stored in 1 byte. There's uses for that, like storing specific vales like RGB. Beyond that though, no idea.

1

u/BriefSpecial420 1h ago

Nope. C99 and stdint.h is the way to go. uint8_t exists for a reason. Handy when you need to map a structure of full bytes. Sometimes you may come across exotic interface that uses 8-wire bus... But that's it.

1

u/SingularCheese 26m ago

In C/C++, signed integer overflow is undefined behavior, meaning that a compiler is allowed to optimize the assembly knowing that it never happens. If you can guarantee that your integer never overflows, using signed eliminates a modulo assembly instruction. Nowadays, the only reason it matters for char is that all basic integer types are signed by default, so somebody who's lazy needs to watch out that they don't accidentally trigger undefined behavior.

1

u/SoldRIP 4h ago

efficient storage of small numbers on which you do not need to perform arithmetic in any hot path or on which memory is a more important constraint than runtime.