一个汉字占3个字符长度?

软件和网站开发以及相关技术探讨
回复
头像
billcalendar
帖子: 138
注册时间: 2012-03-17 11:05

一个汉字占3个字符长度?

#1

帖子 billcalendar » 2012-03-30 14:49

今天学习文件I/O流时发现,一个汉字竟然占3个字符长度。。。这怎么解释?

代码: 全选

#include <stdio.h>
int main()
{
	char b[200];
	FILE *a=fopen("aa","r");
	//if (a==NULL)
	//	printf("Wrong\n");
	b[0]=fgetc(a);
	b[1]=fgetc(a);
	b[2]=fgetc(a);
	b[3]=fgetc(a);
	b[4]=fgetc(a);
	b[5]=fgetc(a);
	b[6]=fgetc(a);
	if(fgetc(a)==EOF) printf("End\n");
	printf("%s",b);
	fclose(a);
	return 0;
}
aa文件的内容是2个汉字,没有回车。
头像
redhatlinux10
帖子: 773
注册时间: 2008-01-22 23:24
来自: 三亚
联系:

Re: 一个汉字占3个字符长度?

#2

帖子 redhatlinux10 » 2012-03-30 15:06

取决于你采用的什么编码。汉字在GB编码中占用2个字节,在UTF-8中占用3个字节。
头像
billcalendar
帖子: 138
注册时间: 2012-03-17 11:05

Re: 一个汉字占3个字符长度?

#3

帖子 billcalendar » 2012-03-30 15:29

redhatlinux10 写了:取决于你采用的什么编码。汉字在GB编码中占用2个字节,在UTF-8中占用3个字节。
是不是所有汉字在UTF-8中都占用3个字节?
头像
xw_y_am
帖子: 3333
注册时间: 2009-05-08 14:18
系统: Arch
来自: 河南新乡
联系:

Re: 一个汉字占3个字符长度?

#4

帖子 xw_y_am » 2012-03-30 15:32

billcalendar 写了:
redhatlinux10 写了:取决于你采用的什么编码。汉字在GB编码中占用2个字节,在UTF-8中占用3个字节。
是不是所有汉字在UTF-8中都占用3个字节?
yes :em03 :em03
Linux 相关链接大杂烩

代码: 全选

if(read) {
    if(practise) return g☘☘d;
    else return w☘☘d;
} else {
    return t☘☘d;
}
chenxing
帖子: 152
注册时间: 2006-01-03 18:23
联系:

Re: 一个汉字占3个字符长度?

#5

帖子 chenxing » 2012-03-31 0:00

billcalendar 写了:
redhatlinux10 写了:取决于你采用的什么编码。汉字在GB编码中占用2个字节,在UTF-8中占用3个字节。
是不是所有汉字在UTF-8中都占用3个字节?
部分罕见汉字可占四个字节
以Wiki模式创建中文Linux文档,欢迎加入 http://linux-wiki.cn
回复