Home > Backend Development > C++ > body text

Why Are Float Arguments Promoted to Double in printf() and Other Variadic Functions?

DDD
Release: 2024-11-19 08:22:02
Original
925 people have browsed it

Why Are Float Arguments Promoted to Double in printf() and Other Variadic Functions?

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template