相关疑难解决方法(0)

为什么printf("%f",0); 给出未定义的行为?

该声明

printf("%f\n",0.0f);
Run Code Online (Sandbox Code Playgroud)

打印0.

但是,声明

printf("%f\n",0);
Run Code Online (Sandbox Code Playgroud)

打印随机值.

我意识到我表现出某种未定义的行为,但我无法弄明白为什么具体.

所有位都为0的浮点值仍然有效float,值为0.
float并且int在我的机器上具有相同的大小(如果它甚至相关).

为什么使用整数文字而不是浮点文字printf会导致此行为?

如果我使用PS,可以看到相同的行为

int i = 0;
printf("%f\n", i);
Run Code Online (Sandbox Code Playgroud)

c c++ printf undefined-behavior implicit-conversion

86
推荐指数
7
解决办法
6931
查看次数

标签 统计

c ×1

c++ ×1

implicit-conversion ×1

printf ×1

undefined-behavior ×1