scanf函数/ printf的双变量C [英] Scanf/Printf double variable C

查看:167
本文介绍了scanf函数/ printf的双变量C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有这个以下code位在C:

Let's say I have this following bit of code in C:

double var;
scanf("%lf", &var);
printf("%lf", var);
printf("%f", var);

这在标准输出'变种'的两倍标准输入变量VAR,然后打印读取。
我明白这就是你如何从stdin读取一个双变量,但我的问题是:
1.为什么会打印带有LF%双?
2.为什么你可以打印与%F双?
3.哪一个更好,正确使用?

It reads from stdin variable 'var' and then prints twice in stdout 'var'. I understand that's how you read a double variable from stdin, but my questions are: 1. Why can you print a double with %lf? 2. Why can you print a double with %f? 3. Which one is better and correct to use?

推荐答案

有关可变参数的功能,如的printf scanf函数,参数提升,例如,任何较小的整数类型都提升到 INT 浮动提升为双击

For variable argument functions like printf and scanf, the arguments are promoted, for example, any smaller integer types are promoted to int, float is promoted to double.

scanf函数采用指针参数,所以推广规则从没有影响。它必须使用%F 浮法* %LF 双*

scanf takes parameters of pointers, so the promotion rule takes no effect. It must use %f for float* and %lf for double*.

的printf 将永远不会看到一个浮动参数,浮动总是晋升为双击。该格式说明%F 。但C99还表示,%LF 相同%F 的printf

printf will never see a float argument, float is always promoted to double. The format specifier is %f. But C99 also says %lf is the same as %f in printf:

C99§7.19.6.1的 fprintf中函数

C99 §7.19.6.1 The fprintf function

(ELL)指定后续的 D I 0 U X X 转换说明适用于长整型 unsigned long int类型参数;其后面的 N 转换说明适用于指针长整型参数;其后面的 C 转换说明适用于所述wint_t 参数;其后面的取值转换说明适用于指针 wchar_t的参数;或有一个如下的 A A 电子电子˚F˚F先按g 转换说明。

l (ell) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long int or unsigned long int argument; that a following n conversion specifier applies to a pointer to a long int argument; that a following c conversion specifier applies to a wint_t argument; that a following s conversion specifier applies to a pointer to a wchar_t argument; or has no effect on a following a, A, e, E, f, F, g, or G conversion specifier.

这篇关于scanf函数/ printf的双变量C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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