Reputation:
From a previous question:
If you attempt to pass a
float
toprintf
, it'll be promoted todouble
beforeprintf
receives it
printf()
is a variadic function right? So does a variadic function promote a float
argument to a double
before passing it?
Upvotes: 44
Views: 7958
Reputation: 25753
Given a function prototype, type float is only automatically promoted1 when used in trailing arguments. Function print uses those:
int printf(const char * restrict format, ...);
1 (Quoted from: ISO/IEC 9899:201x 6.5.2.2 Function calls)
6. the integer promotions are performed on each argument, and arguments that
have type float are promoted to double. These are called the default argument
promotions.
7. The default argument
promotions are performed on trailing arguments.
Upvotes: 17
Reputation: 1
Because the (C99 or C11) standard says so. See answer by 2501.
There are several pragmatical reasons for that: history (first implementations of C have been used for system programming, where floating point operations don't matter), and the fact that on current (tablet, desktop, server...) processors, arithmetic operations on double
are about as efficient as float
(but some cheap microcontrollers don't have any FPU, or can only add float
by hardware, and require a library for every operation on double
). At last, I guess that such a rule enables slightly simpler calling conventions and ABIs.
Think of float
as a sort-of short double
(which of course is illegal in C). A float
is useful mostly when you need to compact memory (and can afford the loss of precision). See also http://floating-point-gui.de/ for more.
Upvotes: 12
Reputation: 158619
Yes, float arguments to variadic function are promoted to double.
The draft C99 standard section 6.5.2.2
Function calls says:
[...]and arguments that have type float are promoted to double. These are called the default argument promotions.[...]
from the draft C++ standard section 5.2.2
Function call:
[...]a floating point type that is subject to the floating point promotion (4.6), the value of the argument is converted to the promoted type before the call. [...]
and section 4.6
:
A prvalue of type float can be converted to a prvalue of type double. The value is unchanged
cppreference covers the default conversions for variadic function in C++ well:
- std::nullptr_t is converted to void*
- float arguments are converted to double as in floating-point promotion
- bool, char, short, and unscoped enumerations are converted to int or wider integer types as in integer promotion
We can see in C and presumably in C++ this conversion was kept around for compatibility with K&R C, from Rationale for International Standard—Programming Languages—C (emphasis mine):
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.
Upvotes: 39
Reputation: 38238
As for the why part of the question, it's simple: the C (and C++) standards consider double
to be the "default" floating point type. Not float
(which is what many of us programmers default to when using floating point numbers).
This can be seen by observing:
3.14
is a double
(if you want a float
, you've got to take an extra step and append an f
)double
by default (for example, sin()
takes a double
; if you want a float
you've got to use sinf()
)With this, it seems more "natural" that a float
would be promoted to double
in a variadic function call, given that double
is the "natural" default in the language.
Upvotes: 24