关于产品scanf()的使用LF符和printf()与LF符? [英] Question about scanf() with lf specifier and printf() with lf specifier?

查看:162
本文介绍了关于产品scanf()的使用LF符和printf()与LF符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习C和我有以下的code:

I am learning C and I have the following code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
  double x;
  printf("x = ");
  scanf("%ld", &x);
  printf("x = %lf\n", x);

  system("PAUSE");  
  return 0;
}

(我用的开发C4.9中,Windows XP SP3)

(I am using Dev C4.9, Windows XP SP3)

当我运行上面的程序,进入5.3;该计划印刷 X = 0.000000

When I run the above program and entered 5.3; the program printed x = 0.000000

谁能解释这是为什么吗?

Can anyone explain why is that, please?

非常感谢。

推荐答案

%LD 格式字符串意味着它预计在读长符号int ,但你传递它,而不是一个双击。您应该使用%LF 格式说明说,你想有一个双击

The %ld format string means that it's expecting to read in a long signed int, but you're passing it instead a double. You should instead use the %lf format specifier to say that you want a double.

请注意,对于 scanf函数需要双击 S(并且要求是缺席浮动 S),而对于的printf %LF 没有效果:既%F %LF 有两个浮动 S中相同的输出和双击取值由于默认参数推广。

Note that for scanf, the l is required for doubles (and is required to be absent for floats), whereas for printf, the l in %lf has no effect: both %f and %lf have the same output for both floats and doubles, due to default argument promotion.

这篇关于关于产品scanf()的使用LF符和printf()与LF符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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