字符常量包含多个字符的一点思考--有疑问

来源:互联网 发布:python代码保护 编辑:程序博客网 时间:2024/06/06 05:14

一。先写段程序来看下机器的大端小端



  1 #include<stdio.h>

  2 /*函数checkCPU判断计算机是使用大端存储还是小端*/

  3 int checkCPU()  

  4 {

  5         union w{

  6                 int a;

  7                 char b;

  8         }c;

  9         c.a = 1;

 10 

 11         return(c.b == 1);   //如果是小端的话,则程序返回1

 12 }

 13 

 14 int main()

 15 {

 16         int b = checkCPU();

 17         if(1 == b)

 18         {

 19                 printf("Little Endian!/n");  //小端

 20         }

 21         else

 22         {

 23                 printf("Big Endian! /n");

 24         }

 25         return 0;

 26 }


编译:gcc testEndian.c -o testEndian

执行结果:
Little Endian!


二。当一个字符常量包含多个字符赋值给一个char类型变量
 说明:在第一部分已经测试了机器是使用小端存储,就是高位存储在高地址,地位存储在低地址

   1 #include<stdio.h>
  2 
  3 int main()
  4 {
  5         char ch;
  6 
  7         ch = '123';     //编译时候有警告warning: multi-character character constant ;warning: overflow in implicit constant conversion
  8         printf("the char ch is %c /n" ,ch);    //输出是3,因为是小端字节序;
  9 
 10         ch = 'abc';    //编译时候有警告
 11         printf("the char ch is %c /n" ,ch);
 12         
 13         return 0;
 14 }

编译:gcc testChar1.c -o testChar1

执行结果:
the char ch is 3 
the char ch is c 


三。一个字符常量包含多个字符,直接打印输出 --不明白?

 1 #include<stdio.h>
  2 
  3 int main()
  4 {
  5         char *pt = '12';  //编译有警告:initialization makes pointer from integer without a cast
                                  //注意:这里不是char *pt = "12";
  6         printf("the bits of the CPU is %d bits/n/n" ,sizeof(pt) * 8);
  7         printf("the adress of pt is %p /n" ,pt);
  8         printf("the content of 'printf(" " ,'12');'is 0x %x:/n/n" ,'12'); //
warning: multi-character character constant
  9 //      printf("%d" ,*pt);  //为什么会有段错误呢??
 10 
 11         char *ps = '56';   //可以理解成把'56'对应的ASCII码对应的十六进制当成地址赋值给指针变量ps??
 12         printf("the adress of ps is %p /n" ,ps);
 13         printf("the content of 'printf(" " ,'56');'is 0x%x :/n/n" ,'56');
 14 
 15         char *pp = '98765';  //warning:
character constant too long for its type ,
 16         printf("the address of ps is %p /n" ,pp);
 17         printf("the content of 'printf(" " ,'98765');'is 0x%x :/n" ,'98765');//
warning: character constant too long for its type

 18         
 19         return 0;
 20 }

编译:
beanu@beanu-laptop:~/work/day10.22$ gcc testChar.c 
testChar.c:5:13: warning: multi-character character constant
testChar.c: In function ‘main’:
testChar.c:5: warning: initialization makes pointer from integer without a cast
testChar.c:8:61: warning: multi-character character constant
testChar.c:11:13: warning: multi-character character constant
testChar.c:11: warning: initialization makes pointer from integer without a cast
testChar.c:13:61: warning: multi-character character constant
testChar.c:15:13: warning: character constant too long for its type
testChar.c:15: warning: initialization makes pointer from integer without a cast
testChar.c:17:62: warning: character constant too long for its type

执行结果:
the bits of the CPU is 32 bits

the adress of pt is 0x3132 
the content of 'printf( ,'12');'is 0x 3132:

the adress of ps is 0x3536 
the content of 'printf( ,'56');'is 0x3536 :

the address of ps is 0x38373635 
the content of 'printf( ,'98765');'is 0x38373635 :
----

个人拙见:printf("%x" , '12');  是打印出来这个地址对应的十六进制数

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 初一数学没救了怎么办 五年级英语不好怎么办 三年级孩子学习差怎么办 孩子三年级学习成绩差怎么办 三年级字写不好怎么办 7小孩表达能力差怎么办 孩子考了低分怎么办 初一考300分怎么办 小学三年级成绩不好怎么办 五年级考几分怎么办 思想晚熟的人怎么办 孩子学不好数学怎么办 做作业速度慢怎么办 孩子碎头发太多怎么办 孩子碎发太多怎么办 孩子的作业太多怎么办 爸爸是乙肝孩子怎么办 孩子出生没有奶怎么办 顺产后没有奶水怎么办 还在经常逃课怎么办 老板不重视自己怎么办 孩子不听话爱撒谎怎么办 老师针对我孩子怎么办 小孩动不动就哭怎么办 孩子太拧应该怎么办 孩子贪玩不爱学习怎么办 高二了还不学怎么办 小朋友不喜欢上幼儿园怎么办 一岁半的宝宝不听话怎么办 老公是个窝囊废怎么办 幼儿园老师不喜欢家长怎么办 老师讨厌学生了怎么办 一个舍友很讨厌怎么办 家长对老师不满怎么办 教师被家长辱骂怎么办 如果老师喜欢自己怎么办 孩子不思进取逃避学习怎么办 老师拿孩子泄愤怎么办 老师对小孩不好怎么办 孩子被老师骂怎么办 孩子幼儿园被打怎么办