char 是單字符類型,長度為一個字節
wchar_t 是寬字符類型,長度為兩個字節,主要用在國際 Unicode 編碼中
舉例:
#include<iostream> using namespace std; int main(void) { char a = 'A'; wchar_t b = L'B'; wchar_t c = L'龍'; cout << a << " -> " << sizeof(a) << endl; // 寬字符輸出用wcout wcout << b << " -> " << sizeof(b) << endl; // C++中默認為EN_US,中文需轉換 wcout.imbue(locale("chs")); wcout << c << " -> " << sizeof(c) << endl; return 0; }
運行結果: