为什么printf()的促进浮到双? [英] Why does printf() promote a float to a double?
问题描述
从previous <一个href=\"http://stackoverflow.com/questions/4264127/correct-format-specifier-for-double-in-printf?rq=1\">question:
From a previous question:
如果你试图传递一个浮动
到的printf
,它会被提升到双
前的printf
接收它
If you attempt to pass a
float
toprintf
, it'll be promoted todouble
beforeprintf
receives it
的printf()
是一个可变参数函数吧?因此,没有一个可变参数函数促进浮动
参数传递给双击
传递之前?
printf()
is a variadic function right? So does a variadic function promote a float
argument to a double
before passing it?
推荐答案
是的,浮动参数可变参数函数都提升一倍。
Yes, float arguments to variadic function are promoted to double.
借助草案C99标准部分 6.5.2.2
函数调用说:
[...]和论据
有float类型都提升到两倍。这些被称为默认参数
促销活动。[...]
[...]and arguments that have type float are promoted to double. These are called the default argument promotions.[...]
从草案C ++标准部分 5.2.2
函数调用:
[...]一个浮点类型,是受浮点
推广(4.6),该参数的值转换为
呼叫之前提升的类型。 [...]
[...]a floating point type that is subject to the floating point promotion (4.6), the value of the argument is converted to the promoted type before the call. [...]
和部分 4.6
:
float类型的prvalue可以转换为double类型的prvalue。的值不变
A prvalue of type float can be converted to a prvalue of type double. The value is unchanged
CP preference 覆盖的默认转换在C可变参数函数的++得好:
- 的std :: nullptr_t被转换为void *
- 浮动参数转换成兼作浮点推广
- 布尔,焦炭,短,无范围的枚举转换成int或更宽的整数类型为整型提升
我们可以在C看到并使用C presumably ++这种转换是围绕保持兼容的 K&放大器; RC 的,从的理由国际标准的编程语言-C (重点煤矿的):
We can see in C and presumably in C++ this conversion was kept around for compatibility with K&R C, from Rationale for International Standard—Programming Languages—C (emphasis mine):
有关以往的惯例兼容性,所有的参数提升为发生
在K&放描述; R在没有原型声明中,包括
在并不总是可取的推广浮动翻番
For compatibility with past practice, all argument promotions occur as described in K&R in the absence of a prototype declaration, including the not always desirable promotion of float to double.
这篇关于为什么printf()的促进浮到双?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!