char vs wchar_t when to use which data type

后端 未结 3 1302
梦谈多话
梦谈多话 2020-12-14 19:31

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 exa

3条回答
  •  鱼传尺愫
    2020-12-14 20:03

    Fundamentally, use wchar_t when the encoding has more symbols than a char can contain.

    Background
    The char type has enough capacity to hold any character (encoding) in the ASCII character set.

    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.

    The wchar_t, a.k.a. wide characters, provides more room for encodings.

    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.

    Prefer Unicode to handle large character sets (such as emojis).

提交回复
热议问题