用C中的scanf读取双重值

发布于 2025-01-26 11:25:17 字数 407 浏览 2 评论 0 原文

我尝试使用C中的scanf()读取2个值,但是系统将内存写入内存的值不等于我的输入值。这是代码:

double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n"); 
scanf("%ld",&b);
printf("%d %d",a,b);

如果我输入1和2,则CMD返回正确,但是B = -858993460这是我已经尝试过的: 使用scanf_s使用float或int而不是double,使用scanf(“%i或%i或%li或%lf或%lf或%e或%e或%g),使用fflush(stdin)清除键盘缓冲区,在b中阅读首先,尝试所有可能的组合。无论我做什么,第二个价值总是错误的

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:

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.

I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

海螺姑娘 2025-02-02 11:25:17

使用%lf 格式指定读读二:

double a;
scanf("%lf",&a);

Wikipedia具有不错的参考指定器。

您需要使用%lf 格式指定符来打印结果:

printf("%lf %lf",a,b);

Use the %lf format specifier to read a double:

double a;
scanf("%lf",&a);

Wikipedia has a decent reference for available format specifiers.

You'll need to use the %lf format specifier to print out the results as well:

printf("%lf %lf",a,b);
离鸿 2025-02-02 11:25:17

据我所知,%d 表示decadic,这是没有小数点的数字。如果要加载双重值,请使用%lf 转换(长float)。对于printf,由于相同的原因,您的值是错误的,%d 仅用于整数(如果您知道自己在做什么)数字。

例子:

double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n"); 
scanf("%lf",&b);
printf("%lf %lf",a,b);

As far as i know %d means decadic which is number without decimal point. if you want to load double value, use %lf conversion (long float). for printf your values are wrong for same reason, %d is used only for integer (and possibly chars if you know what you are doing) numbers.

Example:

double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n"); 
scanf("%lf",&b);
printf("%lf %lf",a,b);
雪花飘飘的天空 2025-02-02 11:25:17

您正在使用错误的格式序列 double ,应使用%lf 而不是%ld

double a;
scanf("%lf",&a);

You are using wrong formatting sequence for double, you should use %lf instead of %ld:

double a;
scanf("%lf",&a);
对风讲故事 2025-02-02 11:25:17

printf 中的格式指定词应为%f for double datatypes,因为 float datatyles最终转换为 double> double printf 中的数据类型。

没有任何规定可以打印 float 数据。请在此处找到讨论:正确的格式指定器

Format specifier in printf should be %f for doubl datatypes since float datatyles eventually convert to double datatypes inside printf.

There is no provision to print float data. Please find the discussion here : Correct format specifier for double in printf

嘦怹 2025-02-02 11:25:17

扫描第二个值时,请使用此代码行:
scanf(“%lf”,& b);
还用%LF替换所有%LD。

这是与输入流缓冲区有关的问题。您也可以使用fflush(stdin);在第一次扫描以清除输入缓冲区后,然后第二个SCANF将按预期工作。另一种方法是放置getch();或getChar();第一个SCANF系列后的功能。

Use this line of code when scanning the second value:
scanf(" %lf", &b);
also replace all %ld with %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.

海拔太高太耀眼 2025-02-02 11:25:17

正确的方法是:

double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);        //%lf is long float numbers 
printf("--------\n"); 
scanf("%lf",&b);
printf("%f %f",a,b);     //%f float number

Correct ways is:

double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);        //%lf is long float numbers 
printf("--------\n"); 
scanf("%lf",&b);
printf("%f %f",a,b);     //%f float number
救星 2025-02-02 11:25:17

使用%LF 将帮助您解决此问题。

使用 :

scanf("%lf",&doub)

Using %lf will help you in solving this problem.

Use :

scanf("%lf",&doub)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文