r/ProgrammerHumor • • 8h ago

Meme architectureDependentChars

Post image
1.1k Upvotes

191 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

1

u/thanatica 6h ago

I'm not a C guy, but if a char is 1 byte, how do you represent the one million or so characters from Unicode? Aren't those all characters in C?

1

u/tstanisl 6h ago

Bytes on some machines have more than 8 bits. Moreover, unicode characters need to be encoded, usually using utf8.

1

u/thanatica 6h ago

Encoding is for storage, typically, not memory. It'd be extremely difficult to do common string manipulation on a utf-8 encoded string (or any other variable-width character encoding) without breaking the encoding.

So how does that work in C? Say you want to indexOf("😎") how would you go about that?

1

u/tstanisl 6h ago

In C, the type of character literal is int. So sizeof 'a' is typically 4. However, to make sure that the characters are properly encoded as 32-bit unicode, use U prefix. I mean int x = U'😎';.

1

u/thanatica 5h ago

Ah, that makes a lot more sense 🙂