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>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
>
15 <H2>2.2 Constants
</H2>
17 <p>[This section corresponds to K
&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
35 </p><p>We write constants in decimal, octal, or hexadecimal
38 The compiler doesn't care;
39 it always converts everything into binary internally, anyway.
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;
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
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,
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
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:
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:
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
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
105 Notice once again that the character constant <TT>'A'</TT>
106 is very different from the string constant <TT>"A
"</TT>.
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>
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>