For an embedded project I'd like to know when does a standard-compliant C-compiler (C99) and C++-compiler (C++11) will most likely implicitly promote a single-float variable/value to double-float.
I'm aware of two cases:
- literals which are not suffixed with
f
. For example:3.14
- passing a float to function with variadic argument-list (
...
)
Are there any others? What about templates?
The answers of this question are very helpful for me as well - including it here for reference.
In C:
A numeric literal with
.
and no suffix, e.g.3.14
, does not involve any promotion. It isdouble
for its entire lifetime.A float is promoted to double if the float is an argument to a function call, and the function being called has no prototype in scope, or the argument corresponds to the ellipsis (
...
) in the prototype in scope.A float is converted to double in any of the following situations:
double
in a prototype in scope.double
andfloat
as the two argument types. The operators this applies to are:* / + - < > <= >= == !=
double
andfloat
as the second and third operand (in either order)double
double
(including compound assignment)In C++, all of the above cases still apply, except for the cases about no prototype (since C++ requires all function calls to have a prototype in scope).
There is a new case: the standard conversion sequence which is too complicated to summarize briefly. But as an example, this C++ code contains an implicit conversion from
float
todouble
:I'm not sure if this is an exhaustive list for C++ though.