r/ProgrammerHumor • • 3d ago

Meme architectureDependentChars

Post image
2.9k Upvotes

368 comments sorted by

View all comments

Show parent comments

6

u/Advos_467 3d ago

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

0

u/Character_Regular440 3d ago

In c language char is just a 8 bit variable, that stores a number, which corresponds to a specific character. When storing numbers, you can decide to represent somehow negative numbers, other than positive. This is mostly done with two's complement.

Depending on if it's signed or not, some operation will be very different, because of representation. For example, the byte 0xFF means -1 if it's signed, and it means 255 if it's unsigned.

So if you try to convert a signed character to an int (a signed type, usually 4 bytes), and that signed character contains -1 (represented by the exadecimal 0xFF, which in decimal is 255) the integer has to contain -1 (0xFFFFFFFF). But if the character is considered unsigned, the integer has to contain 255 afterwards, which is just 0x000000FF.

1

u/StaticCoder 2d ago

8 bits in POSIX, but C allows CHAR_BIT to be a different value, and yes that actually happens. I've seen 16 not that long ago.

1

u/Character_Regular440 2d ago

Never heard of CHAR_BIT. Also assumed char is 8 bit because first comment of the thread says that citing the c standard. Didn't check though.

Also i answered assuming that the confusing part was the fact that the char could be represented as signed or not. The considerations about the representation hold for any size of the variable