Promotion of Float Arguments to Double in printf()
In a previous discussion, it was noted that when a float is passed to printf(), it is promoted to double before being used by the function. This behavior is not specific to printf() but rather to all variadic functions.
In the absence of a prototype declaration, all argument promotions in C and C occur as defined in the K&R C standard. This includes the promotion of float arguments to double, even when not desirable.
Variadic Function Argument Promotion
According to the C99 standard (Section 6.5.2.2), arguments of type float are promoted to double before being passed to variadic functions. Similarly, the C standard (Section 5.2.2) states that floating point arguments are converted to the promoted type (double) before the function call.
cppreference provides a clear overview of the default conversions for variadic functions in C , including the conversion of float arguments to double.
Compatibility with K&R C
The promotion of float arguments to double in variadic functions was retained in C and C for compatibility with the original K&R C standard. Despite concerns about the potential for data loss, this conversion remains in place for backwards compatibility.
The above is the detailed content of Why Are Float Arguments Promoted to Double in printf() and Other Variadic Functions?. For more information, please follow other related articles on the PHP Chinese website!