gpt4 book ai didi

c - 在C中简单使用双数的printf和scanf的问题

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

我做了一个非常简单的程序:

#include <stdio.h>
int main()
{
double x;
printf("Write your number \n");
scanf ("%f", &x);
printf("You've written %f \n", x);

return 0;
}

结果出现了奇怪的数字(无论我给出什么 x):“你写了 8309626105313258000000000000000000000000000000000000000000

这是怎么回事?当我将所有数字更改为“int”类型时,该程序工作正常。

最佳答案

看看在启用警告的情况下编译它时会发生什么:

amb@nimrod-ubuntu:~/so$ gcc -Wall x.c -o x
x.c: In function ‘main’:
x.c:6:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat]

%f 更改为 %lf 并且 scanfprintf 函数将正确地接受

关于c - 在C中简单使用双数的printf和scanf的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26577194/

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