r/ProgrammerHumor • • Oct 04 '26

Meme architectureDependentChars

Post image
3.0k Upvotes

372 comments sorted by

View all comments

Show parent comment

1

u/thanatica Oct 04 '26

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 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 🙂

1

u/gmes78 Oct 05 '26

In C, a char is just one byte; it did correspond to a character while ASCII was being used, but, nowadays, character encodings are multi-byte.

A Unicode grapheme cluster (what you'd intuitively think of as a "character") can be composed of multiple UTF-8 bytes.

1

u/FuckIPLaw Oct 05 '26

Besides: in C you're often still dealing with actual ascii because one of the main uses for C in 2026 is embedded stuff.

1

u/thanatica Oct 06 '26

Then how do you, say, split a string? You do know utf-8 is a variable width encoding, so... Difficult at best?

1

u/gmes78 Oct 06 '26

You use a library to do it. ICU is the reference implementation, but there are other alternatives, such as utf8proc.

1

u/meancoot Oct 05 '26

The char type in C was named way before Unicode came about. char32_t is the type you would use if you needed a character type that could hold any Unicode scalar. char8_t and char16_t are types that represent UTF8 and UTF16 code units. The char type itself is just the C type for a byte.

1

u/HSavinien Oct 05 '26 edited Oct 05 '26

depend. in most case, what mater is the string, not the individual bytes. So as long as it's null terminated, it doesn't matter that a block of 4 'char' actually represent a single character. Your string manipulation work the same, and once you want to print it, it's the terminals problem, not yours (or whatever program you print to).

something like printf("┌──┐🇨\n"); is perfectly valid and will work as expected.

If you do need char accurate string manipulation, you can use variable that are bigger than a single byte. (in which case sizeof do matter)

1

u/thanatica Oct 06 '26

Okay, so it just takes some careful consideration when dealing with strings. Because especially in GUI programs, the chances that a user will input anything outside the 7-bit ASCII range, is extremely high, iyam. Even if not considering astral characters (which we should, for the record).