1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>18.2.3: Default Type Promotions and Conversions
</title>
10 <link href=
"sx4bb.html" rev=precedes
>
11 <link href=
"sx4db.html" rel=precedes
>
12 <link href=
"sx4b.html" rev=subdocument
>
15 <H3>18.2.3: Default Type Promotions and Conversions
</H3>
17 <p>[This section corresponds to the first half of K
&R Sec.
2.7]
20 C performs type conversions automatically when values of
21 differing types participate in expressions.
23 you don't have to memorize these rules exactly,
24 but it's good idea to have a general understanding of how they work,
25 so that you won't be surprised by any of the default conversions,
26 and so that you'll know to use explicit conversions
27 (as described in the previous section)
28 in those few cases where C would not perform
29 a needed conversion automatically.
30 </p><p>The default conversion rules serve two purposes.
31 One is purely selfish on the compiler's part:
32 it does not want to have to know how to generate code to add,
33 say, a floating-point number to an integer.
34 The compiler would much prefer if all operations operated on two
35 values of the same type:
36 two integers, two floating-point numbers, etc.
37 (Indeed, few processors have an instruction for adding a
38 floating-point number to an integer;
39 most have instructions for adding two integers, or two
40 floating-point numbers.)
41 The other purpose for the default conversions is the
42 programmer's convenience:
43 the mentality that ``the computer and the compiler are
44 stupid, we programmers must specify everything in excruciating
45 detail'' can be carried too far, and it's reasonable to
46 define the language such that
47 certain conversions are performed
48 implicitly and automatically
50 when it's unambiguous and safe to do so.
51 </p><p>The rules, then
52 (which you can also find on page
44 of K
&R2,
53 or in section
6.2.1 of the newer ANSI/ISO C Standard)
54 are approximately as follows:
55 <OL><li>First, in most circumstances,
56 values of type
<TT>char
</TT> and
<TT>short int
</TT>
57 are converted to
<TT>int
</TT>
59 <li>If an operation involves two operands,
60 and one of them is of type
<TT>long double
</TT>,
61 the other one is converted to
<TT>long double
</TT>.
62 <li>If an operation involves two operands,
63 and one of them is of type
<TT>double
</TT>,
64 the other one is converted to
<TT>double
</TT>.
65 <li>If an operation involves two operands,
66 and one of them is of type
<TT>float
</TT>,
67 the other one is converted to
<TT>float
</TT>.
68 <li>If an operation involves two operands,
69 and one of them is of type
<TT>long int
</TT>,
70 the other one is converted to
<TT>long int
</TT>.
71 <li>If an operation involves both signed and unsigned integers,
72 the situation is a bit more complicated.
73 If the unsigned operand is smaller
74 (perhaps we're operating on
75 <TT>unsigned int
</TT> and
<TT>long int
</TT>),
76 such that the larger, signed type could represent all values of
77 the smaller, unsigned type,
78 then the unsigned value is converted to the larger, signed type,
79 and the result has the larger, signed type.
82 signed type can
<em>not
</em> represent
83 all values of the unsigned type),
84 both values are converted to
89 and the result has that
91 <li>Finally, when a value is assigned to a variable using the
93 it is automatically converted to the type of the variable if
94 (a) both the value and the variable have arithmetic type
95 (that is, integer or floating point), or
96 (b) both the value and the variable are pointers,
97 and one or the other of them is of type
<TT>void *
</TT>.
98 </OL>(This is
<em>not
</em> a precise statement of these rules.
99 If you need to understand a complicated type conversion situation perfectly,
100 you may have to consult a more definitive reference.
103 these rules are usually described as being applied in order,
104 in the order
2,
3,
4,
1,
5.
105 Rule
6 is especially complicated,
106 and although it is intended to prevent surprises,
107 it still manages to introduce some.)
111 <a href=
"sx4bb.html" rev=precedes
>prev
</a>
112 <a href=
"sx4db.html" rel=precedes
>next
</a>
113 <a href=
"sx4b.html" rev=subdocument
>up
</a>
114 <a href=
"top.html">top
</a>
117 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
118 //
<a href=
"copyright.html">Copyright
</a> 1996-
1999
119 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>