无符号字符型(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)你并没有定义存储该语句分配多大内存,计算机自己计算出双引号之间的字符数目。 |
相关文章推荐
- 解决VS2008 没法解析的外部符号 "int _cdecl _AfxInitManaged(void)“,以及无法打开预编译头Debug\XXX.pch
- 无法在安装新的版本前,为"*"做一个符号链接备份
- 宏定义中的"#"和"##"符号
- 托管c++中的资源管理--- "~"和"!"符号的作用
- 如何将java中的"/"替换为"/"以及replaceAll(String regex, String replacement) 注意事项
- openssl——从内存中读取RSA公钥并加密 以及 "PRNG not seeded" error message 的解决办法
- <%@ include file=""%> 和<jsp:include page=""/>两者的区别
- <%@ page include=""%> 和<jsp:include flush="true" page=""></jsp:include>两者的区别
- "CREATE TABLE AS SELECT"以及"INSERT INTO AS SELECT"语句的优化
- "<br />"和 "\r\n" 这两者有什么区别??
- &&"与“&” 和 “||”与“|” 两者之间的区
- js中的"=="和equals()以及is()三者的区别
- extern "c" 动态链接库符号表导出问题 以及函数参数压栈顺序
- "<br />"和 "\r\n" 这两者有什么区别??
- 关于oracle导出导入以及关于oracle的$符号==SP2-0734: unknown command beginning "exp username..." - rest of line ignored.
- 创建路径,以"\" 或者"//"为最后以及目录
- "链接器工具错误 LNK 2001 无法解析外部符号 __security_cookie " 解决方法
- "=="与equals,以及equals为ture时hash code是否相同
- js中的"=="和equals()以及is()三者的区别
- 写了一个ASP.Net2.0中使用"客户端回调"实现无刷新例子