字符常量包含多个字符的一点思考--有疑问
一。先写段程序来看下机器的大端小端
    
  
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');  是打印出来这个地址对应的十六进制数