如何printf和联合区分beetween float和double [英] How does printf and co differentiate beetween float and double

查看:159
本文介绍了如何printf和联合区分beetween float和double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于它不是强类型的我还以为它只是选择了正确的内存大小,跨preTED它根据参数的类型。但是,float和double都使用%f和他们是不同的大小。

Since it isn't strongly typed I thought it just picked the right memory size and interpreted it based on the type of argument. But float and double both use %f and they are different sizes.

P.S。
我可以看到通过价值复制到一个临时铸造推广如何(这是正确的?)可能工作
但它是如何进行工作scanfs / sscanf的?

P.S. I can see how promotion via copying the value to a temp and casting(is this right?) might work but how does it work for scanfs/sscanf?

推荐答案

它不区分。这是不可能接收浮动作为可变参数:任何浮动参数您提供的第一个晋升为双击

It doesn't differentiate. It's not possible to receive a float as a vararg: any float argument that you provide is first promoted to double.

6.5.2.2/6定义默认参数提升,和/ 7指出默认参数提升应用到尾随参数,即通过可变参数 ...

6.5.2.2/6 defines "default argument promotions", and /7 states that default argument promotions are applied to "trailing arguments", that is varargs denoted by ....

它是如何为scanfs / sscanf的工作?

how does it work for scanfs/sscanf?

%F 的格式为: scanf函数需要一个指向浮动%LF 需要一个指向双击%LF 要求一个指向长双

The %f format for scanf requires a pointer to float. %lf requires a pointer to double, %Lf requires a pointer to long double.

值复制到一个临时铸造(是这样吧?)

copying the value to a temp and casting(is this right?)

如果你提供了一个float参数,则实现创建一个临时的double类型的,用浮点值初始化它,并把它作为可变参数。顾名思义铸造的明确的通过使用强制转换运算符的转换 - 你可以,如果你为了使其准确清楚地向读者发生了什么事情,但像投浮动˚F = 3;的printf(%F,F); 是完全一样的浮动F = 3;的printf(%F(双)F); 。默认参数促销的含义投一样的。

If you provide a float argument, then the implementation creates a temporary of type double, initializes it with the float value, and passes this as the vararg. Casting by definition is explicit conversion by use of the cast operator -- you can cast if you like in order to make it exactly clear to the reader what's going on, but float f = 3; printf("%f", f); is exactly the same as float f = 3; printf("%f", (double)f);. The default argument promotion has the same meaning as the cast.

这篇关于如何printf和联合区分beetween float和double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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