printf displays something weird

后端 未结 5 1316
刺人心
刺人心 2020-12-06 19:26

There is such code:

#include 

int main() {
  float d = 1.0;
  int i = 2;
  printf(\"%d %d\", d, i);
  getchar();
  return 0;
}
5条回答
  •  醉梦人生
    2020-12-06 19:51

    You need to know how printf works. The caller puts all the arguments on the stack. As it parses through the fmt string, the first time it sees a %d it picks the first 4-byte word on the stack and prints it as an integer. The second time it sees a %d, it picks the next 4-byte word. What you're seeing is the raw float bytes being displayed as two integers.

提交回复
热议问题