18.2.3: Default Type Promotions and Conversions
[This section corresponds to the first half of K&R Sec. 2.7]
In many cases,
C performs type conversions automatically when values of
differing types participate in expressions.
For most programming,
you don't have to memorize these rules exactly,
but it's a good idea to have a general understanding of how they work,
so that you won't be surprised by any of the default conversions,
and so that you'll know to use explicit conversions
(as described in the previous section)
in those few cases where C would not perform
a needed conversion automatically.
The default conversion rules serve two purposes.
One is purely selfish on the compiler's part:
it does not want to have to know how to generate code to add,
say, a floating-point number to an integer.
The compiler would much prefer if all operations operated on two
values of the same type:
two integers, two floating-point numbers, etc.
(Indeed, few processors have an instruction for adding a
floating-point number to an integer;
most have instructions for adding two integers, or two
floating-point numbers.)
The other purpose for the default conversions is the
programmer's convenience:
the mentality that ``the computer and the compiler are
stupid, we programmers must specify everything in excruciating
detail'' can be carried too far, and it's reasonable to
define the language such that
certain conversions are performed
implicitly and automatically
by the compiler,
when it's unambiguous and safe to do so.
The rules, then
(which you can also find on page 44 of K&R2,
or in section 6.2.1 of the newer ANSI/ISO C Standard)
are approximately as follows:
- First, in most circumstances,
values of type char and short int
are converted to int
right off the bat.
- If an operation involves two operands,
and one of them is of type long double,
the other one is converted to long double.
- If an operation involves two operands,
and one of them is of type double,
the other one is converted to double.
- If an operation involves two operands,
and one of them is of type float,
the other one is converted to float.
- If an operation involves two operands,
and one of them is of type long int,
the other one is converted to long int.
- If an operation involves both signed and unsigned integers,
the situation is a bit more complicated.
If the unsigned operand is smaller
(perhaps we're operating on
unsigned int and long int),
such that the larger, signed type could represent all values of
the smaller, unsigned type,
then the unsigned value is converted to the larger, signed type,
and the result has the larger, signed type.
Otherwise
(that is, if the
signed type can not represent
all values of the unsigned type),
both values are converted to
a common
unsigned
type,
and the result has that
unsigned type.
- Finally, when a value is assigned to a variable using the
assignment operator,
it is automatically converted to the type of the variable if
(a) both the value and the variable have arithmetic type
(that is, integer or floating point), or
(b) both the value and the variable are pointers,
and one or the other of them is of type void *.
(This is not a precise statement of these rules.
If you need to understand a complicated type conversion situation perfectly,
you may have to consult a more definitive reference.
In particular,
the first five of
these rules are usually described as being applied in order,
in the order 2, 3, 4, 1, 5.
Rule 6 is especially complicated,
and although it is intended to prevent surprises,
it still manages to introduce some.)
Read sequentially:
prev
next
up
top
This page by Steve Summit
// Copyright 1996-1999
// mail feedback