C语言 在 c 中使用 scanf 读取双精度值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13730188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading in double values with scanf in c
提问by user1880009
I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code:
我尝试在 C 中使用 scanf() 读入 2 个值,但系统写入内存的值不等于我输入的值。这是代码:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried: using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that there is a problem with the length of double on 32 bit OS, so that you are forced to use scanf("%lf", &f) to read in a double. No matter what I do, second value is always wrong.
如果我输入 1 和 2,CMD 返回正确,但 b = -858993460 这是我已经尝试过的:使用 float 或 int 而不是 double,使用 scanf_s,使用 scanf("%d or %f for %i or %li或 %lf 或 %e 或 %g ),使用 fflush(stdin) 清除键盘缓冲区,先读入 b,尝试所有可能的组合。我发现 32 位操作系统上 double 的长度有问题,所以你被迫使用 scanf("%lf", &f) 读入一个双精度值。无论我做什么,第二个值总是错误的。
I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.
我在 Windows 7 32 位操作系统上使用 MS VS express 2012 for Desktop。
回答by simonc
Use the %lfformat specifier to read a double:
使用%lf格式说明符读取双精度:
double a;
scanf("%lf",&a);
Wikipedia has a decent referencefor available format specifiers.
维基百科对可用的格式说明符有一个不错的参考。
You'll need to use the %lfformat specifier to print out the results as well:
您还需要使用%lf格式说明符来打印结果:
printf("%lf %lf",a,b);
回答by Kyborek
As far as i know %dmeans decadic which is number without decimal point. if you want to load double value, use %lfconversion (long float). for printf your values are wrong for same reason, %dis used only for integer (and possibly chars if you know what you are doing) numbers.
据我所知%d,十进制是没有小数点的数字。如果要加载双%lf精度值,请使用转换(长浮点数)。对于 printf 您的值出于同样的原因是错误的,%d仅用于整数(如果您知道自己在做什么,则可能是字符)数字。
Example:
例子:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
回答by piokuc
You are using wrong formatting sequence for double, you should use %lfinstead of %ld:
您为 使用了错误的格式序列double,您应该使用%lf代替%ld:
double a;
scanf("%lf",&a);
回答by Biswajit Roy
Format specifier in printfshould be %ffor doubldatatypes since floatdatatyles eventually convert to doubledatatypes inside printf.
格式说明符 inprintf应该%f用于doubl数据类型,因为数据类型float最终会转换为double内部的数据类型printf。
There is no provision to print floatdata. Please find the discussion here : Correct format specifier for double in printf
没有提供打印float数据的规定。请在此处找到讨论: 在 printf 中正确格式说明符为 double
回答by JameSBonD
Use this line of code when scanning the second value:
scanf(" %lf", &b);also replace all %ld with %lf.
扫描第二个值时使用这行代码:
scanf(" %lf", &b);还将所有 %ld 替换为 %lf。
It's a problem related with input stream buffer. You can also use fflush(stdin); after the first scanning to clear the input buffer and then the second scanf will work as expected. An alternate way is place a getch(); or getchar(); function after the first scanf line.
这是与输入流缓冲区相关的问题。您也可以使用 fflush(stdin); 在第一次扫描后清除输入缓冲区,然后第二次 scanf 将按预期工作。另一种方法是放置一个 getch(); 或 getchar(); 第一个 scanf 行之后的函数。
回答by Akshansh
Using %lfwill help you in solving this problem.
使用%lf将帮助您解决这个问题。
Use :
用 :
scanf("%lf",&doub)

