scanf收集了错误的输入 [英] scanf is collecting the wrong input

查看:83
本文介绍了scanf收集了错误的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #include<stdio.h> 
 int main(void)
 {
      double c;
      scanf("%f", &c);
      printf("%f", c);
 }

这是我尝试编写的程序的摘录,但我同样简单的事情也会遇到同样的问题。当我运行此命令并输入 1.0时,它会打印出 0.007812。我看过以前的几个问题,这些问题与我的相似,找不到合适的答案。

This is an exerpt from a program I'm attempting to write, but I get the same issue with something this simple. when I run this, and enter "1.0", it prints out "0.007812". I've looked at several previous questions that were similar to mine and could not find an appropriate answer.

推荐答案

您需要使用%lf 为两倍。

这是clang编译器发出的警告。

This is the warning from clang compiler.


warning:format指定类型为'float *',但参数类型为'double *'[-Wformat]
scanf(%f,& c);

warning: format specifies type 'float *' but the argument has type 'double *' [-Wformat] scanf("%f", &c);

这是扫描引用。格式为%[*] [width] [length]指定符。 浮点数的说明符是 f 。因此,我们使用%f 来读取 float x 。要读取 double x ,我们需要将长度指定为 l 。组合格式为%lf

Here is the scanf reference. It's format is %[*][width][length]specifier. The specifier for 'floating point number' is f. So we use %f to read float x. To read double x, we need to specify the length as l. Combined the format is %lf.

这篇关于scanf收集了错误的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆