1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Character Constants (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Character Constants (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Character Constants (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Constants.html" rel="up" title="Constants">
|
---|
33 | <link href="String-Constants.html" rel="next" title="String Constants">
|
---|
34 | <link href="Invalid-Numbers.html" rel="prev" title="Invalid Numbers">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Character-Constants"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="String-Constants.html" accesskey="n" rel="next">String Constants</a>, Previous: <a href="Invalid-Numbers.html" accesskey="p" rel="prev">Invalid Numbers</a>, Up: <a href="Constants.html" accesskey="u" rel="up">Constants</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Character-Constants-1"></span><h3 class="section">12.6 Character Constants</h3>
|
---|
64 | <span id="index-character-constants"></span>
|
---|
65 | <span id="index-constants_002c-character"></span>
|
---|
66 | <span id="index-escape-sequence"></span>
|
---|
67 |
|
---|
68 | <p>A <em>character constant</em> is written with single quotes, as in
|
---|
69 | <code>'<var>c</var>'</code>. In the simplest case, <var>c</var> is a single ASCII
|
---|
70 | character that the constant should represent. The constant has type
|
---|
71 | <code>int</code>, and its value is the character code of that character.
|
---|
72 | For instance, <code>'a'</code> represents the character code for the letter
|
---|
73 | ‘<samp>a</samp>’: 97, that is.
|
---|
74 | </p>
|
---|
75 | <p>To put the ‘<samp>'</samp>’ character (single quote) in the character
|
---|
76 | constant, <em>quote</em> it with a backslash (‘<samp>\</samp>’). This character
|
---|
77 | constant looks like <code>'\''</code>. This sort of sequence, starting with
|
---|
78 | ‘<samp>\</samp>’, is called an <em>escape sequence</em>—the backslash character
|
---|
79 | here functions as a kind of <em>escape character</em>.
|
---|
80 | </p>
|
---|
81 | <p>To put the ‘<samp>\</samp>’ character (backslash) in the character constant,
|
---|
82 | quote it likewise with ‘<samp>\</samp>’ (another backslash). This character
|
---|
83 | constant looks like <code>'\\'</code>.
|
---|
84 | </p>
|
---|
85 | <span id="index-bell-character"></span>
|
---|
86 | <span id="index-_005ca"></span>
|
---|
87 | <span id="index-backspace"></span>
|
---|
88 | <span id="index-_005cb"></span>
|
---|
89 | <span id="index-tab-_0028ASCII-character_0029"></span>
|
---|
90 | <span id="index-_005ct"></span>
|
---|
91 | <span id="index-vertical-tab"></span>
|
---|
92 | <span id="index-_005cv"></span>
|
---|
93 | <span id="index-formfeed"></span>
|
---|
94 | <span id="index-_005cf"></span>
|
---|
95 | <span id="index-newline"></span>
|
---|
96 | <span id="index-_005cn"></span>
|
---|
97 | <span id="index-return-_0028ASCII-character_0029"></span>
|
---|
98 | <span id="index-_005cr"></span>
|
---|
99 | <span id="index-escape-_0028ASCII-character_0029"></span>
|
---|
100 | <span id="index-_005ce"></span>
|
---|
101 | <p>Here are all the escape sequences that represent specific
|
---|
102 | characters in a character constant. The numeric values shown are
|
---|
103 | the corresponding ASCII character codes, as decimal numbers.
|
---|
104 | </p>
|
---|
105 | <div class="example">
|
---|
106 | <pre class="example">'\a' ⇒ 7 /* <span class="roman">alarm, <kbd>CTRL-g</kbd></span> */
|
---|
107 | '\b' ⇒ 8 /* <span class="roman">backspace, <tt class="key">BS</tt>, <kbd>CTRL-h</kbd></span> */
|
---|
108 | '\t' ⇒ 9 /* <span class="roman">tab, <tt class="key">TAB</tt>, <kbd>CTRL-i</kbd></span> */
|
---|
109 | '\n' ⇒ 10 /* <span class="roman">newline, <kbd>CTRL-j</kbd></span> */
|
---|
110 | '\v' ⇒ 11 /* <span class="roman">vertical tab, <kbd>CTRL-k</kbd></span> */
|
---|
111 | '\f' ⇒ 12 /* <span class="roman">formfeed, <kbd>CTRL-l</kbd></span> */
|
---|
112 | '\r' ⇒ 13 /* <span class="roman">carriage return, <tt class="key">RET</tt>, <kbd>CTRL-m</kbd></span> */
|
---|
113 | '\e' ⇒ 27 /* <span class="roman">escape character, <tt class="key">ESC</tt>, <kbd>CTRL-[</kbd></span> */
|
---|
114 | '\\' ⇒ 92 /* <span class="roman">backslash character, <kbd>\</kbd></span> */
|
---|
115 | '\'' ⇒ 39 /* <span class="roman">singlequote character, <kbd>'</kbd></span> */
|
---|
116 | '\"' ⇒ 34 /* <span class="roman">doublequote character, <kbd>"</kbd></span> */
|
---|
117 | '\?' ⇒ 63 /* <span class="roman">question mark, <kbd>?</kbd></span> */
|
---|
118 | </pre></div>
|
---|
119 |
|
---|
120 | <p>‘<samp>\e</samp>’ is a GNU C extension; to stick to standard C, write ‘<samp>\33</samp>’.
|
---|
121 | </p>
|
---|
122 | <p>You can also write octal and hex character codes as
|
---|
123 | ‘<samp>\<var>octalcode</var></samp>’ or ‘<samp>\x<var>hexcode</var></samp>’. Decimal is not an
|
---|
124 | option here, so octal codes do not need to start with ‘<samp>0</samp>’.
|
---|
125 | </p>
|
---|
126 | <p>The character constant’s value has type <code>int</code>. However, the
|
---|
127 | character code is treated initially as a <code>char</code> value, which is
|
---|
128 | then converted to <code>int</code>. If the character code is greater than
|
---|
129 | 127 (<code>0177</code> in octal), the resulting <code>int</code> may be negative
|
---|
130 | on a platform where the type <code>char</code> is 8 bits long and signed.
|
---|
131 | </p>
|
---|
132 | <hr>
|
---|
133 | <div class="header">
|
---|
134 | <p>
|
---|
135 | Next: <a href="String-Constants.html" accesskey="n" rel="next">String Constants</a>, Previous: <a href="Invalid-Numbers.html" accesskey="p" rel="prev">Invalid Numbers</a>, Up: <a href="Constants.html" accesskey="u" rel="up">Constants</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
136 | </div>
|
---|
137 |
|
---|
138 |
|
---|
139 |
|
---|
140 | </body>
|
---|
141 | </html>
|
---|