手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

用%f占位符输出整形变量的问题

时间:2024-10-15 12:23:12

1、下文是C11标准的文档:坡纠课柩if type is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), the behavior is undefined, except for the following cases:— one type is a signed integer type, the other type is the corresponding unsigned integer type, and the value is representable in both types;— one type is pointer to void and the other is a pointer to a character type.由此我们可以知道,用类型不匹配的占位符格式化输出一个变量的行为是未定义的(int类型与unsigned int类型以及指向void类型的指针类型和指向字符型的指针类型除外),下面我们就来分析这种未定义的行为

2、我们都知道(如果不知道请参阅《深入理解计算机系统》第二章),int类型的变量是在计算机中是以补码形式存储的,以十进制数字10为例,在计算机中它的存储形式应当为00000000 00000000 00000000 00001010,然而,单精度浮点数的存储形式为第一位表示该数字的符号,随后的8位标识其指数部分,剩下的23位表示其尾数部分,所以,当以补码形式存储的十进制整形数字10被以单精度浮点型解释的时候,它就变成了+1*10^(-20)+1*10^(-22),这个数字非常接近于0,而单精度浮点型的尾数部分默认为6位,将该数值的精度设定为小数点后六位的时候,它的数值就变成0了。

© 手抄报圈