linux下的wchar_t

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,应该能说明问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的幸运儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值