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.2: Cast Operators
</title>
10 <link href=
"sx4ab.html" rev=precedes
>
11 <link href=
"sx4cb.html" rel=precedes
>
12 <link href=
"sx4b.html" rev=subdocument
>
15 <H3>18.2.2: Cast Operators
</H3>
17 <p>[This section corresponds to the second half of K
&R Sec.
2.7]
18 </p><p>Most of the time,
19 C performs conversions between related types automatically.
23 for the complete story.)
24 When you assign an
<TT>int
</TT> value to a
<TT>float
</TT> variable
26 or perform calculations involving mixtures of arithmetic types,
27 the types are converted automatically, as necessary.
28 C even performs some pointer conversions automatically:
29 <TT>malloc
</TT> returns type
<TT>void *
</TT>
30 (pointer-to-
<TT>void
</TT>),
31 but a
<TT>void *
</TT> is automatically converted
32 to whatever pointer type you assign
34 <TT>malloc
</TT>'s return value to.
35 </p><p>Occasionally, you need to request a type conversion explicitly.
42 Recall that the division operator
<TT>/
</TT>
43 results in an integer division,
44 discarding the remainder,
45 when both operands are integral.
46 It performs a floating-point division,
47 yielding a possibly fractional result,
48 when one or both operands have floating-point types.
50 Both operands are
<TT>int
</TT>,
51 but the result of the division is assigned to a
<TT>float
</TT>,
52 which would be able to hold a fractional result.
53 Is the compiler smart enough to notice,
54 and perform a floating-point division?
56 The rule is, ``if both operands are integral,
57 division is integer division and discards any remainder'',
58 and this is the rule the compiler follows.
60 we must manually and explicitly force one of the operands
61 to be of floating-point type.
62 </p><p>Explicit type conversions are requested in C
63 using a
<dfn>cast operator
</dfn>.
64 (The name of the operator comes from the term
<dfn>typecast
</dfn>;
65 ``typecasting'' is another term for explicit type conversion,
66 and some languages have ``typecast operators.''
67 Yet another term for type conversion is
<dfn>coercion
</dfn>.)
68 A cast operator consists of a type name, in parentheses.
69 One way to fix the example above would be to rewrite it as
73 The construction
<TT>(float)i
</TT> involves a cast;
74 it says, ``take
<TT>i
</TT>'s value,
75 and convert it to a
<TT>float
</TT>.''
76 (The only thing being converted is the value fetched from
<TT>i
</TT>;
77 we're not changing
<TT>i
</TT>'s type or anything.)
78 Now, one operand of the
<TT>/
</TT> operator
<em>is
</em> floating-point,
79 so we perform a floating-point division,
80 and
<TT>f
</TT> receives the value
0.5.
81 </p><p>Equivalently, we could write
87 f = (float)i / (float)j;
89 It's sufficient to use a cast on one of the operands,
93 doesn't hurt to cast both.
95 </p><p>A similar situation is
97 int i =
32000, j =
32000;
101 An
<TT>int
</TT> is only guaranteed to hold values up to
32,
767.
102 Here, the result
<TT>i + j
</TT> is
64,
000,
103 which is not guaranteed to fit into an int.
104 Even though the eventual destination is a
<TT>long int
</TT>,
105 the compiler does not look ahead to see this.
106 The addition is performed using
<TT>int
</TT> arithmetic,
109 the solution is to use a cast
110 to explicitly convert one of the operands to a
<TT>long int
</TT>:
112 li = (long int)i + j;
114 Now, since one of the operands is a
<TT>long int
</TT>,
115 the addition is performed using
<TT>long int
</TT> arithmetic,
116 and does not overflow.
117 </p><p>Cast operators do not have to involve simple types;
118 they can also involve pointer or structure or more complicated types.
120 before the
<TT>void *
</TT> type had been invented,
121 <TT>malloc
</TT> returned a
<TT>char *
</TT>,
122 which had to be converted to the type you were using.
125 one used to write things like
127 int *iarray = (int *)malloc(
100 * sizeof(int));
131 struct list *lp = (struct list *)malloc(sizeof(struct list));
133 These casts are not necessary under an ANSI C compiler
134 (because
<TT>malloc
</TT> returns
<TT>void *
</TT>
135 which the compiler converts automatically),
136 but you may still see them in older code.
142 <a href=
"sx4ab.html" rev=precedes
>prev
</a>
143 <a href=
"sx4cb.html" rel=precedes
>next
</a>
144 <a href=
"sx4b.html" rev=subdocument
>up
</a>
145 <a href=
"top.html">top
</a>
148 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
149 //
<a href=
"copyright.html">Copyright
</a> 1996-
1999
150 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>