字符常量包含多个字符的一点思考--有疑问
来源:互联网 发布: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'); 是打印出来这个地址对应的十六进制数
- 字符常量包含多个字符的一点思考--有疑问
- 字符常量包含多个字符的一点思考--有疑问
- 字符替换(包含多个字符的替换)
- 字符串常量、字符数组、字符指针的一点小结
- 有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部字符复制成为另一个字符串。
- 字符常量的类型
- 字符常量的引用
- 传入一字符串,判断是否包含中文字符,判断有多少个中文字符
- Working Practice-对于有疑问的问题多一点分析
- 有一字符串,包含n个字符。写一函数,将此字符串中从第m个字符开始的全部复制成为另一个字符串。
- 包含有中文字符、英文字符和数字字符的字符串,请统计各种字符的个数。
- 字符数组和字符常量的区别
- 字符常量
- 字符常量
- 字符常量
- 字符常量
- 关于Java常量定义的一点思考
- 关于Java常量定义的一点思考
- 【通用权限管理】角色的分类管理,角色-用户组-职位职务-系统角色的设计上的迷惑也解开
- cc.Format("%s人/c%05d.bmp",dir,p)
- magento转移服务器和magento建立多站点总结
- SQL Server 聚集索引和非聚集索引的区别总结
- Gosub!
- 字符常量包含多个字符的一点思考--有疑问
- 天地图使用体验
- SQL Server全文索引关于varchar与nvarchar的问题
- keil软件汇编为啥不能生成HEX文件
- 3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
- 3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
- 3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
- 学习汇编的重要意义(转自汇编网)
- 3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】