Home >Backend Development >C++ >Why does Printf() promote floats to doubles?
Deep dive: Why Printf() promotes floating point numbers to double precision floating point numbers
Why Printf() promotes float parameters to double, not just because it is a variadic function. According to the C Standard and the C Standard, all float arguments to variadic functions are promoted to double before being passed.
Section 6.5.2.2 of the C99 standard clearly states: "[...] Floating-point type parameters are promoted to double. These are called default parameter promotions. [...]"
C Draft Standard Section 5.2.2 The section also clarifies: "[...] If a floating-point type parameter is affected by floating-point promotion (4.6), the parameter value will be converted to the promoted type before the call. [...]"
Additionally, section 4.6 states: "A pr value of type float can be converted to a pr value of type double. The value remains unchanged."
Cppreference summarizes C well The default conversion of variable parameter functions: "
In C, this conversion is for compatibility with the K&R C International Standard (Programming Language - C) specification. It clearly states: "For compatibility with past practice, in the absence of prototype declarations, all parameter promotions are performed as described by K&R, including float to double promotions ." "
The above is the detailed content of Why does Printf() promote floats to doubles?. For more information, please follow other related articles on the PHP Chinese website!