Pav*_*ath 0 c floating-point printf hex format-specifiers
我有这个简单的程序
#include <stdio.h>
int main(void)
{
unsigned int a = 0x120;
float b = 1.2;
printf("%X %X\n", b, a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期待输出
some-value 120 (some-value will depend on the bit pattern of `float b` )
Run Code Online (Sandbox Code Playgroud)
但我明白了
40000000 3FF33333
Run Code Online (Sandbox Code Playgroud)
为什么a搞砸的价值?%X把其作为参数signed int,因此它应该检索从堆叠的4个字节并打印的calue b然后获取下一个4个字节打印值的a哪个是0x120
首先,将参数传递给printf不匹配格式说明符是未定义的行为.
其次,当传递给它时,它float被提升为,所以它是八个字节而不是四个字节.哪些字节被解释为格式所期望的两个值取决于推送参数的顺序.doubleprintfunsignedprintf