In Steven Prata\'s book \"C Primer Plus\", there\'s a section on Type Conversions, wherein \"The basic rules are\" section has stated in rule 1:
Under K&R C,
The C language definition has been standardized and modified several times over the years. The original (non-standardized) version of C is known as "K&R C"; it's the language Kernighan and Ritchie originally developed.
In 1989, ANSI created an official standards document (adopted in 1990 by ISO) to define the language, and in that standard some things were changed and extended; one of the changes is that the automatic promotion from float
to double
was removed.
Since then, there have been two revisions to the standard, one in 1999 and one in 2011.
I'm trying understand if I have an expression that mixes floats and doubles, can I rely on C to promote floats to doubles when it's evaluated?
Here's the rule from the current standard:
6.3.1.8 Usual arithmetic conversions
...
First, if the corresponding real type of either operand islong double
, the other operand is converted, without change of type domain, to a type whose corresponding real type islong double
.
Otherwise, if the corresponding real type of either operand isdouble
, the other operand is converted, without change of type domain, to a type whose corresponding real type isdouble
.
Otherwise, if the corresponding real type of either operand isfloat
, the other operand is converted, without change of type domain, to a type whose corresponding real type isfloat
.62)
62) For example, addition of adouble _Complex
and afloat
entails just the conversion of thefloat
operand todouble
(and yields adouble _Complex
result).
So basically, if you have an expression with two different types, the operand with the narrower/less precise type will be promoted to the type of the operand with the wider/more precise type.