source: public/doc/gnu-c/String-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.9 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>String Constants (GNU C Language Manual)</title>
23
24<meta name="description" content="String Constants (GNU C Language Manual)">
25<meta name="keywords" content="String 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="UTF_002d8-String-Constants.html" rel="next" title="UTF-8 String Constants">
34<link href="Character-Constants.html" rel="prev" title="Character Constants">
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="String-Constants"></span><div class="header">
59<p>
60Next: <a href="UTF_002d8-String-Constants.html" accesskey="n" rel="next">UTF-8 String Constants</a>, Previous: <a href="Character-Constants.html" accesskey="p" rel="prev">Character Constants</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="String-Constants-1"></span><h3 class="section">12.7 String Constants</h3>
64<span id="index-string-constants"></span>
65<span id="index-constants_002c-string"></span>
66
67<p>A <em>string constant</em> represents a series of characters. It starts
68with &lsquo;<samp>&quot;</samp>&rsquo; and ends with &lsquo;<samp>&quot;</samp>&rsquo;; in between are the contents of
69the string. Quoting special characters such as &lsquo;<samp>&quot;</samp>&rsquo;, &lsquo;<samp>\</samp>&rsquo; and
70newline in the contents works in string constants as in character
71constants. In a string constant, &lsquo;<samp>'</samp>&rsquo; does not need to be quoted.
72</p>
73<p>A string constant defines an array of characters which contains the
74specified characters followed by the null character (code 0). Using
75the string constant is equivalent to using the name of an array with
76those contents. In simple cases, the length in bytes of the string
77constant is one greater than the number of characters written in it.
78</p>
79<p>As with any array in C, using the string constant in an expression
80converts the array to a pointer (see <a href="Pointers.html">Pointers</a>) to the array&rsquo;s
81first element (see <a href="Accessing-Array-Elements.html">Accessing Array Elements</a>). This pointer will
82have type <code>char *</code> because it points to an element of type
83<code>char</code>. <code>char *</code> is an example of a type designator for a
84pointer type (see <a href="Pointer-Type-Designators.html">Pointer Type Designators</a>). That type is used
85for strings generally, not just the strings expressed as constants
86in a program.
87</p>
88<p>Thus, the string constant <code>&quot;Foo!&quot;</code> is almost
89equivalent to declaring an array like this
90</p>
91<div class="example">
92<pre class="example">char string_array_1[] = {'F', 'o', 'o', '!', '\0' };
93</pre></div>
94
95<p>and then using <code>string_array_1</code> in the program. There
96are two differences, however:
97</p>
98<ul>
99<li> The string constant doesn&rsquo;t define a name for the array.
100
101</li><li> The string constant is probably stored in a read-only area of memory.
102</li></ul>
103
104<p>Newlines are not allowed in the text of a string constant. The motive
105for this prohibition is to catch the error of omitting the closing
106&lsquo;<samp>&quot;</samp>&rsquo;. To put a newline in a constant string, write it as
107&lsquo;<samp>\n</samp>&rsquo; in the string constant.
108</p>
109<p>A real null character in the source code inside a string constant
110causes a warning. To put a null character in the middle of a string
111constant, write &lsquo;<samp>\0</samp>&rsquo; or &lsquo;<samp>\000</samp>&rsquo;.
112</p>
113<p>Consecutive string constants are effectively concatenated. Thus,
114</p>
115<div class="example">
116<pre class="example">&quot;Fo&quot; &quot;o!&quot; <span class="roman">is equivalent to</span> &quot;Foo!&quot;
117</pre></div>
118
119<p>This is useful for writing a string containing multiple lines,
120like this:
121</p>
122<div class="example">
123<pre class="example">&quot;This message is so long that it needs more than\n&quot;
124&quot;a single line of text. C does not allow a newline\n&quot;
125&quot;to represent itself in a string constant, so we have to\n&quot;
126&quot;write \\n to put it in the string. For readability of\n&quot;
127&quot;the source code, it is advisable to put line breaks in\n&quot;
128&quot;the source where they occur in the contents of the\n&quot;
129&quot;constant.\n&quot;
130</pre></div>
131
132<p>The sequence of a backslash and a newline is ignored anywhere
133in a C program, and that includes inside a string constant.
134Thus, you can write multi-line string constants this way:
135</p>
136<div class="example">
137<pre class="example">&quot;This is another way to put newlines in a string constant\n\
138and break the line after them in the source code.&quot;
139</pre></div>
140
141<p>However, concatenation is the recommended way to do this.
142</p>
143<p>You can also write perverse string constants like this,
144</p>
145<div class="example">
146<pre class="example">&quot;Fo\
147o!&quot;
148</pre></div>
149
150<p>but don&rsquo;t do that&mdash;write it like this instead:
151</p>
152<div class="example">
153<pre class="example">&quot;Foo!&quot;
154</pre></div>
155
156<p>Be careful to avoid passing a string constant to a function that
157modifies the string it receives. The memory where the string constant
158is stored may be read-only, which would cause a fatal <code>SIGSEGV</code>
159signal that normally terminates the function (see <a href="Signals.html">Signals</a>. Even
160worse, the memory may not be read-only. Then the function might
161modify the string constant, thus spoiling the contents of other string
162constants that are supposed to contain the same value and are unified
163by the compiler.
164</p>
165<hr>
166<div class="header">
167<p>
168Next: <a href="UTF_002d8-String-Constants.html" accesskey="n" rel="next">UTF-8 String Constants</a>, Previous: <a href="Character-Constants.html" accesskey="p" rel="prev">Character Constants</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>
169</div>
170
171
172
173</body>
174</html>
Note: See TracBrowser for help on using the repository browser.