r/ProgrammerHumor • • Oct 04 '26

Meme architectureDependentChars

Post image
3.0k Upvotes

372 comments sorted by

View all comments

Show parent comment

1

u/tstanisl Oct 04 '26

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

1

u/thanatica Oct 04 '26

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?

2

u/tstanisl Oct 04 '26

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 Oct 04 '26

Ah, that makes a lot more sense 🙂