I know that letters/one-digit-numbers can be stored in chars,
char letter = 'c';
char number = '1';
but can emojis or foreign letters be stored in a char? If not, how can I store them? Is this possible without strings?
A char
is typically 8 bits. It may be signed or unsigned (it's up to the compiler), so may have any integer value from -128
to 127
(for signed) or 0
to 255
(for unsigned). If a character can be encoded in that range then it can be stored in a single char
variable.
There's also wide characters (wchar_t
) whose size depends again on compiler and operating system. They are usually at least 16 bits.
Then there are explicit Unicode characters, char8_t
for UTF-8 encoded characters (will be added in the C++23 standard, so might not be widely available yet), char16_t
for 16-bit characters in UTF-16 encoding, and char32_t
for 32-bit characters in UTF-32 encoding.
For emojis, or just Unicode characters in general, a single char
is usually not enough. Use either (multiple) char
s/char8_t
s in UTF8 encoding, or use (possibly multiple) char16_t
s, or use char32_t
. Or, if you're targeting Windows and using the Windows API, they use 16-bit wchar_t
for UTF-16 encoded characters.