* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes.int / sx4ca.html
blobe010869ab4f859cc9f70425ef64643343c157d51
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.1.3: <TT>unsigned</TT> integers</title>
10 <link href="sx4ba.html" rev=precedes>
11 <link href="sx4da.html" rel=precedes>
12 <link href="sx4a.html" rev=subdocument>
13 </head>
14 <body>
15 <H3>18.1.3: <TT>unsigned</TT> integers</H3>
17 <p>For
18 each of the integral types,
19 there is a corresponding <TT>unsigned</TT> type.
20 Thus, we have <TT>unsigned char</TT>,
21 <TT>unsigned short int</TT>,
22 <TT>unsigned int</TT>,
23 and
24 <TT>unsigned long int</TT>.
25 There are two differences between the <TT>unsigned</TT> types
26 and the default, signed types:
27 <OL><li>They do not hold negative numbers;
28 their range is from 0 up to some maximum.
29 <li>They have guaranteed properties on overflow.
30 If the range of <TT>unsigned int</TT> on some machine is 0-65,535,
31 and if an <TT>unsigned int</TT> variable contains the value 65,535,
32 then adding 1 to it will wrap around to 0.
33 Whenever a calculation involving <TT>unsigned</TT> integers overflows,
34 or tries to go negative,
35 the result is the remainder that would be obtained if the true
36 (mathematical)
37 result were divided by the range of the <TT>unsigned</TT> type.
38 In other words,
39 if <TT>UINT_MAX</TT> is the largest value
40 that will fit in an <TT>unsigned int</TT>
41 (that is, if the range is 0-<TT>UINT_MAX</TT>),
42 then the results of calculations that would overflow,
43 such as
44 <pre>
45 65535 + 1
46 and
47 5 - 10
48 </pre>
49 are actually
50 <pre>
51 (65535 + 1) % (UINT_MAX+1)
52 and
53 (5 - 10) % (UINT_MAX+1)
54 </pre>
56 </OL>
57 </p><p>The guaranteed minimum ranges of the unsigned types are:
58 <br>
59 <br>
60 <pre>
61 <TT>unsigned char</TT> 0 - 255
62 <TT>unsigned short int</TT> 0 - 65535
63 <TT>unsigned int</TT> 0 - 65535
64 <TT>unsigned long int</TT> 0 - 4294967295
65 </pre>
66 </p><p>These multiword type names can also be abbreviated.
67 Instead of writing <TT>long int</TT>, you can write <TT>long</TT>.
68 Instead of writing <TT>short int</TT>, you can write <TT>short</TT>.
69 Instead of writing <TT>unsigned int</TT>,
70 you can write <TT>unsigned</TT>.
71 Instead of writing <TT>unsigned long int</TT>,
72 you can write <TT>unsigned long</TT>.
73 Instead of writing <TT>unsigned short int</TT>,
74 you can write <TT>unsigned short</TT>.
75 </p><p>In the absence of the <TT>unsigned</TT> keyword,
76 types
77 <TT>short int</TT>, <TT>int</TT>, and <TT>long int</TT>
78 are all signed.
79 However, depending on the particular compiler you're using,
80 plain type <TT>char</TT> might be signed or unsigned.
81 If you need an explicitly signed character type,
82 you can use <TT>signed char</TT>.
84 </p><hr>
85 <p>
86 Read sequentially:
87 <a href="sx4ba.html" rev=precedes>prev</a>
88 <a href="sx4da.html" rel=precedes>next</a>
89 <a href="sx4a.html" rev=subdocument>up</a>
90 <a href="top.html">top</a>
91 </p>
92 <p>
93 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
94 // <a href="copyright.html">Copyright</a> 1996-1999
95 // <a href="mailto:scs@eskimo.com">mail feedback</a>
96 </p>
97 </body>
98 </html>