linux 下的 wchar

    技术2025-10-12  7

    1. 默认情况下,windows 下的 wchar_t 占两个字节的长度,而 linux 下的 wchar_t 占四个字节的长度,可以在使用 gcc 编译程序的时候再后面跟上 -fshort-wchar 来解决这个问题。

    2. linux 下 wchar_t* 字符串的输出问题 —— 没有解决。

    3. 如下程序,可输出宽字符,但是如果加上 -fshort-wchar 编译选项,则输出为乱码。

    #include <wchar.h> #include <locale.h> #include <stdlib.h> #include <stdio.h> #include <string.h> int main(void) { setlocale(LC_ALL,"zh_CN.UTF-8"); wchar_t a[10] = L"你好"; wprintf(L"this is a test !/n"); wprintf(L"%d/n",wcslen(a)); wprintf(L"%ls/n",a); retur 0; }

    4. 可以使用下面这个函数,输出经参数 -fshort-wchar 编译过的宽字符。

    void print_wcs( const wchar_t *text ) { int len = 0; int i = 0; wchar_t *p = NULL; if( NULL == text ) return; p = text; while( *p != L'/0' ) printf( "%lc", *p++ ); }

    最新回复(0)