Home >Backend Development >C++ >Do All Variadic Functions Promote Float Arguments to Double?
Variadic Function Promotion of Float Arguments to Double in printf()
In C and C , the printf() function is a variadic function that takes a variable number of arguments. One peculiar behavior of printf() is that when a float argument is passed, it is converted to double before the function receives it. This raises the question: do all variadic functions promote float arguments to double?
Answer:
Yes, variadic functions in C and C promote float arguments to double.
Justification:
This behavior is specified in both the C99 and C standards. The draft C99 standard specifies that "arguments that have type float are promoted to double" for variadic functions. Similarly, the C standard states that "a floating point type that is subject to the floating point promotion, the value of the argument is converted to the promoted type before the call."
The rationale behind this promotion is to maintain compatibility with the K&R C standard, which required float arguments to be promoted to double in the absence of a prototype declaration. Although not always desirable, this compatibility was preserved for backward compatibility reasons.
The above is the detailed content of Do All Variadic Functions Promote Float Arguments to Double?. For more information, please follow other related articles on the PHP Chinese website!