您的位置:首页 > 其它

无符号字符型(unsigned char)和".2X"(和一般"%5.2f"不一)以及结合两者的例子

2014-11-30 14:31 246 查看
一、unsigned char 类型声明说,长度是1个字节(也就是 8 位 二进制数位),无符号位,8 位都是数值位。所以 unsigned char 数值范围 是 0000 0000 到 1111 1111,  写成 10 进制 是 0 到 255。无符号字符型 实际上 是 1个字节 无符号整型。可以用来描述 ASCII 字符 编码 0 到 255。
二、printf("%.2X",255); //X代表大写十六进制,x代表小写十六进制
输出:FF
printf("%.2X",12);
输出:0C
结合两者的例子:

How will you show memory representation of C variables?

Write a C program to show memory representation of C variables like int, float, pointer, etc. Algorithm: Get the address and size of the variable. Typecast the address to char pointer. Now loop for size of the variable and print the value at the typecasted pointer. Program:
#include <stdio.h> #include <string.h> #define PRAISE "What a super marvelous name!" int main(void) { char name[40] = "Morgan"; printf("strlen = %d\nsizeof = %d\n",strlen(name), sizeof(name)); printf("strlen = %d\nsizeof = %d",strlen(PRAISE), sizeof(PRAISE)); return 0; } 输出结果strlen = 6 sizeof = 40 strlen = 28 sizeof = 29 根据sizeof报告,name有40个字节,不过根据strlen报告只用了其中前6个单元来存放Morgan,第七个字节为空字符,它的存在告诉strlen在哪里停止计数对于 PRAISE ,strlen给出了准确数目(包括空格和标点符号)sizeof运算结果比strlen的结果大1,这是因为它把结束字符串也算进去了(/0)你并没有定义存储该语句分配多大内存,计算机自己计算出双引号之间的字符数目。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐