gpt4 book ai didi

c - 如果我用 %f 打印 int 为什么我会得到 0.000000

转载 作者:行者123 更新时间:2023-11-30 16:50:33 24 4
gpt4 key购买 nike

我不知道为什么如果我用 %f 打印整数变量值,我会得到 0.000000 的返回。

`int main()
{
int age = 18;
printf("%f",age);
return 0;
}`

根据我的想法,它应该返回18.000000因为我正在以 float 格式打印整数值。

最佳答案

这是未定义的行为,因为当您在 C 中使用错误的格式说明符时。

C99 标准 7.19.6.1 第 9 段说:

If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

关于c - 如果我用 %f 打印 int 为什么我会得到 0.000000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42179594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com