* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx2b.html
blob9be831f5ecaaf4273649cb936862096b09f3fc04
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.2 Constants</title>
10 <link href="sx2a.html" rev=precedes>
11 <link href="sx2c.html" rel=precedes>
12 <link href="sx2.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>2.2 Constants</H2>
17 <p>[This section corresponds to K&amp;R Sec. 2.3]
18 </p><p></p><p>A <dfn>constant</dfn> is just an immediate, absolute value
19 found in an expression.
20 The simplest constants are decimal integers,
21 e.g. <TT>0</TT>, <TT>1</TT>, <TT>2</TT>, <TT>123</TT>
23 Occasionally it is useful to specify constants in base 8 or base 16
24 (octal or hexadecimal);
25 this is done by prefixing an extra <TT>0</TT> (zero) for octal,
26 or <TT>0x</TT> for hexadecimal:
27 the constants <TT>100</TT>, <TT>0144</TT>, and <TT>0x64</TT>
28 all represent the same number.
29 (If you're not using these non-decimal constants,
30 just remember not to use any leading zeroes.
31 If you accidentally write <TT>0123</TT>
32 intending to get one hundred and twenty three,
33 you'll get 83 instead, which is
34 123 base 8.)
35 </p><p>We write constants in decimal, octal, or hexadecimal
36 for our convenience,
37 not the compiler's.
38 The compiler doesn't care;
39 it always converts everything into binary internally, anyway.
40 (There is, however,
41 no good way to specify constants in source code in binary.)
42 </p><p>A constant can be forced to be of type <TT>long int</TT>
43 by suffixing it with the letter <TT>L</TT>
44 (in upper or lower case, although upper case is strongly
45 recommended, because a lower case <TT>l</TT> looks too much like
46 the digit <TT>1</TT>).
47 </p><p>A constant that contains a decimal point or the letter
48 <TT>e</TT> (or both) is a floating-point constant:
49 <TT>3.14</TT>, <TT>10.</TT>, <TT>.01</TT>,
50 <TT>123e4</TT>, <TT>123.456e7</TT>
52 The <TT>e</TT> indicates multiplication by a power of 10;
53 <TT>123.456e7</TT>
55 123.456
56 times 10 to the 7th,
58 1,234,560,000.
59 (Floating-point constants are of type <TT>double</TT> by default.)
60 </p><p>We also have constants for specifying characters and strings.
61 (Make sure you understand the difference between a character
62 and a string:
63 a character is exactly one character;
64 a string is a set of zero or more characters;
65 a string containing one character is distinct from a lone character.)
66 A character constant is simply a single character between
67 single quotes: <TT>'A'</TT>, <TT>'.'</TT>, <TT>'%'</TT>.
68 The numeric value of a character constant is,
69 naturally enough,
70 that character's value in the machine's character set.
71 (In ASCII, for example, <TT>'A'</TT> has the value 65.)
72 </p><p>A <dfn>string</dfn> is represented in C as a
73 sequence or
74 array of characters.
75 (We'll have more to say about arrays in general,
76 and strings in particular, later.)
77 A string constant is a sequence of zero or more characters
78 enclosed in double quotes:
79 <TT>"apple"</TT>,
80 <TT>"hello, world"</TT>,
81 <TT>"this is a test"</TT>.
82 </p><p>Within character and string constants,
83 the backslash character <TT>\</TT> is special,
84 and is used to represent characters
85 not easily typed on the keyboard
86 or for various reasons not easily typed in constants.
87 The most common of these ``character escapes'' are:
88 <br>
89 <br>
90 <pre>
91 <TT>\n</TT> a ``newline'' character
92 <TT>\b</TT> a backspace
93 <TT>\r</TT> a carriage return (without a line feed)
94 <TT>\'</TT> a single quote (e.g. in a character constant)
95 <TT>\"</TT> a double quote (e.g. in a string constant)
96 <TT>\\</TT> a single backslash
97 </pre>
98 </p><p>For example,
99 <TT>"he said \"hi\""</TT> is a string constant
100 which contains two double quotes,
102 <TT>'\''</TT> is a character constant consisting of a
103 (single)
104 single quote.
105 Notice once again that the character constant <TT>'A'</TT>
106 is very different from the string constant <TT>"A"</TT>.
108 </p><hr>
110 Read sequentially:
111 <a href="sx2a.html" rev=precedes>prev</a>
112 <a href="sx2c.html" rel=precedes>next</a>
113 <a href="sx2.html" rev=subdocument>up</a>
114 <a href="top.html">top</a>
115 </p>
117 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
118 // <a href="copyright.html">Copyright</a> 1995-1997
119 // <a href="mailto:scs@eskimo.com">mail feedback</a>
120 </p>
121 </body>
122 </html>