* X more docs for C
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes.int / sx4bb.html
blobaefa14a4785b655711319945e8311001f700c641
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
15 <H3>18.2.2: Cast Operators</H3>
17 <p>[This section corresponds to the second half of K&amp;R Sec. 2.7]
18 </p><p>Most of the time,
19 C performs conversions between related types automatically.
20 (See
22 section 18.2.3
23 for the complete story.)
24 When you assign an <TT>int</TT> value to a <TT>float</TT> variable
25 or vice versa,
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
33 (say)
34 <TT>malloc</TT>'s return value to.
35 </p><p>Occasionally, you need to request a type conversion explicitly.
36 Consider the code
37 <pre>
38 int i = 1, j = 2;
39 float f;
40 f = i / j;
41 </pre>
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.
49 What happens here?
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?
55 No, it is not.
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.
59 In this case, then,
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
70 <pre>
71 f = (float)i / j;
72 </pre>
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
82 <pre>
83 f = i / (float)j;
84 </pre>
86 <pre>
87 f = (float)i / (float)j;
88 </pre>
89 It's sufficient to use a cast on one of the operands,
90 but
92 certainly
93 doesn't hurt to cast both.
95 </p><p>A similar situation is
96 <pre>
97 int i = 32000, j = 32000;
98 long int li;
99 li = i + j;
100 </pre>
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,
107 and it may overflow.
108 Again,
109 the solution is to use a cast
110 to explicitly convert one of the operands to a <TT>long int</TT>:
111 <pre>
112 li = (long int)i + j;
113 </pre>
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.
119 Once upon a time,
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.
124 For example,
125 one used to write things like
126 <pre>
127 int *iarray = (int *)malloc(100 * sizeof(int));
128 </pre>
130 <pre>
131 struct list *lp = (struct list *)malloc(sizeof(struct list));
132 </pre>
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.
139 </p><hr>
141 Read sequentially:
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>
146 </p>
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>
151 </p>
152 </body>
153 </html>