linux 下的 wchar_t

标签: linuxnullwindowsgcc
  7448人阅读  评论(0)  收藏  举报
  分类:

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

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

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

[cpp]  view plain  copy
  1. #include <wchar.h>     
  2. #include <locale.h>     
  3. #include <stdlib.h>     
  4. #include <stdio.h>     
  5. #include <string.h>      
  6. int main(void)      
  7. {      
  8.     setlocale(LC_ALL,"zh_CN.UTF-8");    
  9.     wchar_t a[10] = L"你好";  
  10.     wprintf(L"this is a test !/n");    
  11.     wprintf(L"%d/n",wcslen(a));      
  12.     wprintf(L"%ls/n",a);      
  13.     retur 0;  
  14. }  

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

[cpp]  view plain  copy
  1. void print_wcs( const wchar_t *text )  
  2. {  
  3.     int         len = 0;  
  4.     int      i   = 0;  
  5.     wchar_t  *p  = NULL;  
  6.       
  7.     if( NULL == text )  
  8.         return;  
  9.       
  10.     p = text;  
  11.     while( *p != L'/0' )  
  12.         printf( "%lc", *p++ );  
  13. }  

Logo

更多推荐