source: public/doc/gnu-c/Character-Constants.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.2 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
70character that the constant should represent. The constant has type
71<code>int</code>, and its value is the character code of that character.
72For instance, <code>'a'</code> represents the character code for the letter
73&lsquo;<samp>a</samp>&rsquo;: 97, that is.
74</p>
75<p>To put the &lsquo;<samp>'</samp>&rsquo; character (single quote) in the character
76constant, <em>quote</em> it with a backslash (&lsquo;<samp>\</samp>&rsquo;). This character
77constant looks like <code>'\''</code>. This sort of sequence, starting with
78&lsquo;<samp>\</samp>&rsquo;, is called an <em>escape sequence</em>&mdash;the backslash character
79here functions as a kind of <em>escape character</em>.
80</p>
81<p>To put the &lsquo;<samp>\</samp>&rsquo; character (backslash) in the character constant,
82quote it likewise with &lsquo;<samp>\</samp>&rsquo; (another backslash). This character
83constant 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
102characters in a character constant. The numeric values shown are
103the corresponding ASCII character codes, as decimal numbers.
104</p>
105<div class="example">
106<pre class="example">'\a' &rArr; 7 /* <span class="roman">alarm, <kbd>CTRL-g</kbd></span> */
107'\b' &rArr; 8 /* <span class="roman">backspace, <tt class="key">BS</tt>, <kbd>CTRL-h</kbd></span> */
108'\t' &rArr; 9 /* <span class="roman">tab, <tt class="key">TAB</tt>, <kbd>CTRL-i</kbd></span> */
109'\n' &rArr; 10 /* <span class="roman">newline, <kbd>CTRL-j</kbd></span> */
110'\v' &rArr; 11 /* <span class="roman">vertical tab, <kbd>CTRL-k</kbd></span> */
111'\f' &rArr; 12 /* <span class="roman">formfeed, <kbd>CTRL-l</kbd></span> */
112'\r' &rArr; 13 /* <span class="roman">carriage return, <tt class="key">RET</tt>, <kbd>CTRL-m</kbd></span> */
113'\e' &rArr; 27 /* <span class="roman">escape character, <tt class="key">ESC</tt>, <kbd>CTRL-[</kbd></span> */
114'\\' &rArr; 92 /* <span class="roman">backslash character, <kbd>\</kbd></span> */
115'\'' &rArr; 39 /* <span class="roman">singlequote character, <kbd>'</kbd></span> */
116'\&quot;' &rArr; 34 /* <span class="roman">doublequote character, <kbd>&quot;</kbd></span> */
117'\?' &rArr; 63 /* <span class="roman">question mark, <kbd>?</kbd></span> */
118</pre></div>
119
120<p>&lsquo;<samp>\e</samp>&rsquo; is a GNU C extension; to stick to standard C, write &lsquo;<samp>\33</samp>&rsquo;.
121</p>
122<p>You can also write octal and hex character codes as
123&lsquo;<samp>\<var>octalcode</var></samp>&rsquo; or &lsquo;<samp>\x<var>hexcode</var></samp>&rsquo;. Decimal is not an
124option here, so octal codes do not need to start with &lsquo;<samp>0</samp>&rsquo;.
125</p>
126<p>The character constant&rsquo;s value has type <code>int</code>. However, the
127character code is treated initially as a <code>char</code> value, which is
128then converted to <code>int</code>. If the character code is greater than
129127 (<code>0177</code> in octal), the resulting <code>int</code> may be negative
130on a platform where the type <code>char</code> is 8 bits long and signed.
131</p>
132<hr>
133<div class="header">
134<p>
135Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.