1. 默认情况下,windows 下的 wchar_t 占两个字节的长度,而 linux 下的 wchar_t 占四个字节的长度,可以在使用 gcc 编译程序的时候再后面跟上 -fshort-wchar 来解决这个问题。
2. linux 下 wchar_t* 字符串的输出问题 —— 没有解决。
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
void main()
{
char *sc= "short characters";
wchar_t *wc= L"wider characters";
printf("%s are more universally useful than %s, ",wc,sc);
printf("but they do use more space(%d as opposed to %d bytes).\n",
wcslen(wc)*sizeof(wchar_t),strlen(sc));
printf("the wchart size:%d\n",sizeof(wchar_t));
printf("the wc len:%d\n",wcslen(wc));
exit(0);
}
没有加- fshort-wchar情况下的运行结果:
wider characters are more universally useful than short characters, but they do use more space(64 as opposed to 16 bytes).
the wchart size:4
the wc len:16
添加-fshort-wchar编译选项后,运行结果:
but they do use more space(16 as opposed to 16 bytes).
the wchart size:2
the wc len:8
3.如下程序,可输出宽字符,但是如果加上 -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++ );
}
主程序添加如下代码:
printf("==========\n");
print_wcs(wc);
在添加-fshort-wchar之后的运行结果:
but they do use more space(16 as opposed to 16 bytes).
the wchart size:2
the wc len:8
==========
wider characters
ok,应该能说明问题。