"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Do All Variadic Functions Promote `float` Arguments to `double`?

Do All Variadic Functions Promote `float` Arguments to `double`?

Published on 2024-11-16
Browse:687

Do All Variadic Functions Promote `float` Arguments to `double`?

Promotion of Float Argument to Double in Variadic Functions

In a previous discussion, it was noted that printf() promotes a float argument to a double before accepting it. This raises the question: do all variadic functions perform this promotion?

Variadic Argument Promotion

Yes, according to both the C99 and C standards, float arguments passed to variadic functions are promoted to double.

Standard Specifications

  • C99 (section 6.5.2.2): "arguments that have type float are promoted to double."
  • C (section 5.2.2): "a floating point type... is converted to the promoted type before the call."
  • C (section 4.6): "A prvalue of type float can be converted to a prvalue of type double. The value is unchanged."

cppreference on Variadic Function Conversions

cppreference provides a summary of the default conversions for variadic functions in C :

  • std::nullptr_t to void*
  • Float arguments to double
  • Integer promotion for bool, char, short, and enumerations

Origin of Promotion

This promotion was introduced in C for compatibility with the K&R C standard, despite its potential drawbacks. As the C Rationale states:

"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."

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3