我有这段代码输出错误的结果。
#include <stdio.h>
#include <string.h>
int main()
{
unsigned char bytes[4];
float flt=0;
bytes[0]=0xde;
bytes[1]=0xad;
bytes[2]=0xbe;
bytes[3]=0xef;
memcpy( &flt, bytes, 4);
printf("bytes 0x%x float %e\n", flt, flt);
return 0;
}
我得到的输出是
字节 0xc0000000 浮点数 -2.000001e+00
我期待得到
字节 0xdeadbeef 浮点数 -6.2598534e+18
编辑#1,正如指出的那样,字节顺序可能不同,这将导致以下结果
字节 0xefbeadde 浮点数 -1.1802469e+29
我不明白的是从 float 转换为 unsigned int 导致 0xc0000000 (同一 printf 语句中的 float 为 -2.0000 我将归因于编译器优化)
这是以前在另一台计算机上工作的。这可能是架构更改。