scanf 不起作用.需要从控制台读取 double [英] scanf not working. need to read double from console

查看:28
本文介绍了scanf 不起作用.需要从控制台读取 double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我做错了什么,但我无法从控制台读取双精度值.由于某种原因,阅读它可以正常工作.我正在使用 Xcode.

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.

顺便说一句,clang 会在没有任何额外标志的情况下对此发出警告,即使使用 -Wall -Wextra -pedantic,gcc 4.6 也不会对此发出警告.

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 不起作用.需要从控制台读取 double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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