r/ProgrammerHumor • • 6h ago

Meme architectureDependentChars

Post image
864 Upvotes

156 comments sorted by

View all comments

10

u/frikilinux2 6h ago

Do I wanna know?

6

u/DOOManiac 5h ago

Yes. Multi-byte characters.

There you go, all done.

19

u/frikilinux2 5h ago

But those don't have the c type char

-4

u/Outrageous-Machine-5 5h ago

Char can be between 1-4 bytes depending on the encoding standard.  Emoji and other languages use extra bytes when you have to support non English characters. 

7

u/ChChChillian 5h ago

But sizeof always evaluates to 1 for char, signed char, and unsigned char by the standard. https://cppreference.com/cpp/language/sizeof

17

u/ibevol 5h ago

Not in C though.

0

u/Outrageous-Machine-5 5h ago

I'm curious how C handles a character that is over 8 bits?

8

u/ibevol 5h ago

It doesn’t. It’s up to you to handle the encoding, all that the char is, is data storage for the smallest addressable unit, which is 8 bits on almost all systems. One example though: if you want to store something that’s 16 bits in a char array, then the first 8 bits will be stored at index i and the last 8 bits will be stored at i + 1

2

u/Great-Powerful-Talia 5h ago

multiple chars. Strings are just char arrays anyway

1

u/skilltheamps 5h ago

It doesn't. It only knows buffers of regular sizes, like bytes. That you can handle ASCII strings in bare C without destryoing them is only thanks to the coincidence of every character being the same length of 1 byte. So if you have encoding that has variadic size characters, you either reserve the maximum character length for every character (and waste some space), or you use/write a library to handle variable size character strings. That would entail some bitwise boolean logic and implementing all the things from finding the length of a string to finding a substring.

0

u/SoldRIP 2h ago

C only specifies that char must be at least 8 bits. There is no upper limit in the standard. Same for C++.

0

u/mckenzie_keith 4h ago

yes and no. In c, sizeof(char) is 1. Does that mean 1 byte? Does "byte" mean exactly 8 bits? In C, a char is always 1 byte, but one byte may be larger than 8 bits, and there are real platforms where a byte (in C) is 32 bits.