博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个汉字真的由两个字节存放吗?
阅读量:4152 次
发布时间:2019-05-25

本文共 1401 字,大约阅读时间需要 4 分钟。

偶尔间发现了一个程序,让我想起了一个问题:一个汉字真的由两个字节存放吗?

先看一个程序段:

//文件名字为test.c
#include
#include
#include
int main(int argc, char *argv[])
{
    char ch2;
    FILE *fp;
    int i;
    fp = fopen(argv[1], "rt");
    if(fp == NULL) {
        perror("fopen:");
    }
    while((ch2 = fgetc(fp)) != EOF) {
        printf("%c", ch2);
    }
    fclose(fp);
    return 0;
}
在命令行中进行如下命令:
gcc test.c
./a.out sun.txt
注:假如在同目录下有一个文件sun.txt,里面内容为:
天亮了
上面的执行结果将为:
天亮了
如果将上面的程序改为如下:
//文件名字为test.c
#include
#include
#include
int main(int argc, char *argv[])
{
    char ch1[20] = {0}, ch2;
    FILE *fp;
    int i = 0, j;
    fp = fopen(argv[1], "rt");
    if(fp == NULL) {
        perror("fopen:");
    }
    while((ch2 = fgetc(fp)) != EOF) {
        printf("%c", ch2);
        ch1[i++] = ch2;
    }
    i = strlen(ch1);
    for(j = 0; j < i - 1; j++) {
        printf("ch1[%d]=%c\n", j, ch1[j]);
    }
    printf("%c%c%c\n", ch1[0], ch1[1], ch1[2]);
    printf("%c%c\n", ch1[0], ch1[1]);
    printf("%c%c\n", ch1[1], ch1[2]);
    fclose(fp);
    return 0;
}
在命令行中进行如下命令:
gcc test.c
./a.out sun.txt
注:假如在同目录下有一个文件sun.txt,里面内容为:
天亮了
上面的执行结果将为:
天亮了
ch1[0]=
ch1[1]=
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(1597) | 评论(0) | 转发(0) |
0

上一篇:

下一篇:

相关热门文章
给主人留下些什么吧!~~
评论热议

转载地址:http://wcmti.baihongyu.com/

你可能感兴趣的文章
异或的妙用
查看>>
google笔试
查看>>
printf
查看>>
C语言中的数据类型及其转换详解
查看>>
c类型转换
查看>>
位运算
查看>>
螺旋矩阵
查看>>
旋转有序数组的二分查找
查看>>
vector.resize
查看>>
python
查看>>
cout的输出顺序
查看>>
楼层扔鸡蛋
查看>>
linux删除空行 基本操作
查看>>
各种正则
查看>>
正则表达式shell
查看>>
python作用域
查看>>
重载赋值运算符
查看>>
返回值优化
查看>>
vim复制
查看>>
输出所有的合法的括号组合
查看>>