Home > Backend Development > C++ > Do All Variadic Functions Promote `float` Arguments to `double`?

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

Barbara Streisand
Release: 2024-11-23 06:21:16
Original
744 people have browsed it

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

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template