char vs wchar_t何时使用哪种数据类型 [英] char vs wchar_t when to use which data type
问题描述
我想了解 char
和 wchar_t
之间的区别吗?我知道 wchar_t
使用更多字节,但是我可以得到一个清晰的示例来区分何时使用 char
vs wchar_t
I want to understand the difference between char
and wchar_t
? I understand that wchar_t
uses more bytes but can I get a clear cut example to differentiate when I would use char
vs wchar_t
推荐答案
从根本上说,使用 wchar_t
编码的符号数超过 char
可以包含的数量时。
Fundamentally, use wchar_t
when the encoding has more symbols than a char
can contain.
背景
char
类型的容量足以保留ASCII字符集中的任何字符(编码)。
Background
The char
type has enough capacity to hold any character (encoding) in the ASCII character set.
问题在于,许多语言需要比ASCII帐户更多的编码。因此,需要更多而不是127种可能的编码。某些语言具有256种以上的可能编码。 char
类型不能保证范围大于256。因此需要新的数据类型。
The issue is that many languages require more encodings than the ASCII accounts for. So, instead of 127 possible encodings, more are needed. Some languages have more than 256 possible encodings. A char
type does not guarantee a range greater than 256. Thus a new data type is required.
wchar_t
,又称宽字符,为编码提供了更多空间。
The wchar_t
, a.k.a. wide characters, provides more room for encodings.
摘要
在范围内使用 char
数据类型的编码小于或等于256,例如ASCII。当您需要大于256的容量时,请使用 wchar_t
。
Summary
Use char
data type when the range of encodings is 256 or less, such as ASCII. Use wchar_t
when you need the capacity for more than 256.
建议使用Unicode处理大字符集(例如表情符号)。
Prefer Unicode to handle large character sets (such as emojis).
这篇关于char vs wchar_t何时使用哪种数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!