scanf函数无法正常工作。需要读取从控制台的双 [英] scanf not working. need to read double from console

查看:142
本文介绍了scanf函数无法正常工作。需要读取从控制台的双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我做错了什么,但我无法从控制台读取一个double。读取它由于某种原因,工作正常。我用X code。

I'm not sure what I'm doing wrong, but I'm not able to read a double from the console. Reading an it works fine for some reason. I'm using Xcode.

double n1;

// get input from the user
printf("Enter first number: ");
scanf("%f", &n1);

printf("%f", n1);

这将始终打印0不管我进去。

This will always print 0 no matter what I enter.

推荐答案

%F 正在寻找一个浮动,而不是双。如果你想使用双,使用格式%LF

%f is looking for a float, not a double. If you want to use a double, use the format %lf.

作为一个有些有趣之余,铛警告这个没有任何额外的标志,GCC 4.6甚至不会与 -Wall -pedantic -Wextra 提醒一下吧。

As a somewhat interesting aside, clang warns about this without any extra flags, gcc 4.6 won't warn about it even with -Wall -Wextra -pedantic.

这篇关于scanf函数无法正常工作。需要读取从控制台的双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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