* X more docs for C
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx2a.html
blobb28dc0b90619d06b7d46ddaebccb1059cb27e5a4
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>2.1 Types</title>
10 <link href="sx2.html" rev=precedes>
11 <link href="sx2b.html" rel=precedes>
12 <link href="sx2.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>2.1 Types</H2>
17 <p>[This section corresponds to K&amp;R Sec. 2.2]
18 </p><p>There are only a few basic data types in C.
19 The first ones we'll be
20 encountering and
21 using are:
23 <UL><li><csyntax>char</>
24 a character
25 <li><csyntax>int</>
26 an integer, in the range -32,767 to 32,767
27 <li><csyntax>long int</>
28 a larger integer
29 (up to +-2,147,483,647)
30 <li><csyntax>float</>
31 a floating-point number
32 <li><csyntax>double</>
33 a floating-point number,
34 with more precision and perhaps greater range than <TT>float</TT>
35 </UL></p><p>If you can look at this list of
36 basic types
37 and say to yourself,
38 ``Oh, how simple, there are only a few types,
39 I won't have to worry much about choosing among them,''
40 you'll have an easy time with declarations.
41 (Some masochists
43 wish that the type system were more complicated
44 so that they could specify more things about each variable,
45 but those of us who would rather not have to specify these
46 extra things each time are glad that we don't have to.)
47 </p><p>The ranges listed above for types <TT>int</TT> and
48 <TT>long int</TT> are the guaranteed minimum ranges.
49 On some systems, either of these types
50 (or, indeed, any C type)
51 may be able to hold larger values,
52 but a program that depends on
53 extended ranges
54 will not be as portable.
55 Some programmers
57 become obsessed with knowing exactly what
58 the sizes of data objects will be in various situations,
59 and go on to write programs which depend on these exact sizes.
60 Determining or controlling the size of an object is
61 occasionally important,
62 but most of the time we can sidestep size issues
63 and let the compiler do most of the worrying.
64 </p><p>(From the ranges listed above,
65 we can determine that
66 type <TT>int</TT> must be at least 16 bits,
67 and that type <TT>long int</TT> must be at least 32 bits.
68 But neither of these sizes is exact;
69 many systens have 32-bit <TT>int</TT>s,
70 and some systems have 64-bit <TT>long int</TT>s.)
71 </p><p>You might wonder how the computer stores characters.
72 The answer involves a <dfn>character set</dfn>,
73 which is simply a mapping between some set of characters
74 and some set of small numeric codes.
75 Most machines today use the ASCII character set,
76 in which the letter A is represented by the code 65,
77 the ampersand &amp; is represented by the code 38,
78 the digit 1 is represented by the code 49,
79 the space character is represented by the code 32,
80 etc.
81 (Most of the time,
82 of course,
83 you have <em>no</em> need to know or even worry about
84 these particular code values;
85 they're automatically translated into
86 the right shapes on the screen or printer when characters are printed out,
87 and they're automatically generated
88 when you type characters on the keyboard.
89 Eventually, though,
90 we'll appreciate,
91 and even take some control over,
92 exactly when these translations--from
93 characters to their numeric codes--are
94 performed.)
95 Character codes are usually small--the
96 largest code value in ASCII is 126,
97 which is the ~ (tilde or circumflex) character.
98 Characters usually fit in a byte, which is usually 8 bits.
99 In C, type <TT>char</TT> is defined as occupying one byte,
100 so it is usually 8 bits.
101 </p><p>Most of the simple variables in most programs are of types
102 <TT>int</TT>, <TT>long int</TT>, or <TT>double</TT>.
103 Typically, we'll use <TT>int</TT> and <TT>double</TT> for most
104 purposes, and <TT>long int</TT> any time we need to hold integer values
105 greater than 32,767.
106 As we'll see,
107 even when we're manipulating individual characters,
108 we'll usually use an <TT>int</TT> variable,
110 reasons to be discussed later.
111 Therefore,
112 we'll
113 rarely use individual variables of type <TT>char</TT>;
114 although we'll use plenty of arrays of <TT>char</TT>.
115 </p><hr>
117 Read sequentially:
118 <a href="sx2.html" rev=precedes>prev</a>
119 <a href="sx2b.html" rel=precedes>next</a>
120 <a href="sx2.html" rev=subdocument>up</a>
121 <a href="top.html">top</a>
122 </p>
124 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
125 // <a href="copyright.html">Copyright</a> 1995-1997
126 // <a href="mailto:scs@eskimo.com">mail feedback</a>
127 </p>
128 </body>
129 </html>