gpt4 book ai didi

c - 关于带有 lf 说明符的 scanf() 和带有 lf 说明符的 printf() 的问题?

转载 作者:太空宇宙 更新时间:2023-11-04 02:17:36 25 4
gpt4 key购买 nike

我正在学习 C,我有以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
double x;
printf("x = ");
scanf("%ld", &x);
printf("x = %lf\n", x);

system("PAUSE");
return 0;
}

(我使用的是 Dev C4.9,Windows XP SP3)

当我运行上面的程序,进入5.3;程序打印出 x = 0.000000

有人能解释一下这是为什么吗?

非常感谢。

最佳答案

%ld格式字符串意味着它期望读入 long signed int ,但您传递的是 double .您应该改为使用 %lf格式说明符表示您想要 double .

注意对于 scanf , l double 是必需的s(对于 float s 必须不存在),而对于 printf , l%lf没有效果:两者都是%f%lf float 的输出相同s 和 double s,由于默认参数提升。

关于c - 关于带有 lf 说明符的 scanf() 和带有 lf 说明符的 printf() 的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5215303/

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