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>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 | <!--
|
---|
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="String-Constants"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <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> [<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
|
---|
68 | with ‘<samp>"</samp>’ and ends with ‘<samp>"</samp>’; in between are the contents of
|
---|
69 | the string. Quoting special characters such as ‘<samp>"</samp>’, ‘<samp>\</samp>’ and
|
---|
70 | newline in the contents works in string constants as in character
|
---|
71 | constants. In a string constant, ‘<samp>'</samp>’ does not need to be quoted.
|
---|
72 | </p>
|
---|
73 | <p>A string constant defines an array of characters which contains the
|
---|
74 | specified characters followed by the null character (code 0). Using
|
---|
75 | the string constant is equivalent to using the name of an array with
|
---|
76 | those contents. In simple cases, the length in bytes of the string
|
---|
77 | constant 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
|
---|
80 | converts the array to a pointer (see <a href="Pointers.html">Pointers</a>) to the array’s
|
---|
81 | first element (see <a href="Accessing-Array-Elements.html">Accessing Array Elements</a>). This pointer will
|
---|
82 | have 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
|
---|
84 | pointer type (see <a href="Pointer-Type-Designators.html">Pointer Type Designators</a>). That type is used
|
---|
85 | for strings generally, not just the strings expressed as constants
|
---|
86 | in a program.
|
---|
87 | </p>
|
---|
88 | <p>Thus, the string constant <code>"Foo!"</code> is almost
|
---|
89 | equivalent 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
|
---|
96 | are two differences, however:
|
---|
97 | </p>
|
---|
98 | <ul>
|
---|
99 | <li> The string constant doesn’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
|
---|
105 | for this prohibition is to catch the error of omitting the closing
|
---|
106 | ‘<samp>"</samp>’. To put a newline in a constant string, write it as
|
---|
107 | ‘<samp>\n</samp>’ in the string constant.
|
---|
108 | </p>
|
---|
109 | <p>A real null character in the source code inside a string constant
|
---|
110 | causes a warning. To put a null character in the middle of a string
|
---|
111 | constant, write ‘<samp>\0</samp>’ or ‘<samp>\000</samp>’.
|
---|
112 | </p>
|
---|
113 | <p>Consecutive string constants are effectively concatenated. Thus,
|
---|
114 | </p>
|
---|
115 | <div class="example">
|
---|
116 | <pre class="example">"Fo" "o!" <span class="roman">is equivalent to</span> "Foo!"
|
---|
117 | </pre></div>
|
---|
118 |
|
---|
119 | <p>This is useful for writing a string containing multiple lines,
|
---|
120 | like this:
|
---|
121 | </p>
|
---|
122 | <div class="example">
|
---|
123 | <pre class="example">"This message is so long that it needs more than\n"
|
---|
124 | "a single line of text. C does not allow a newline\n"
|
---|
125 | "to represent itself in a string constant, so we have to\n"
|
---|
126 | "write \\n to put it in the string. For readability of\n"
|
---|
127 | "the source code, it is advisable to put line breaks in\n"
|
---|
128 | "the source where they occur in the contents of the\n"
|
---|
129 | "constant.\n"
|
---|
130 | </pre></div>
|
---|
131 |
|
---|
132 | <p>The sequence of a backslash and a newline is ignored anywhere
|
---|
133 | in a C program, and that includes inside a string constant.
|
---|
134 | Thus, you can write multi-line string constants this way:
|
---|
135 | </p>
|
---|
136 | <div class="example">
|
---|
137 | <pre class="example">"This is another way to put newlines in a string constant\n\
|
---|
138 | and break the line after them in the source code."
|
---|
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">"Fo\
|
---|
147 | o!"
|
---|
148 | </pre></div>
|
---|
149 |
|
---|
150 | <p>but don’t do that—write it like this instead:
|
---|
151 | </p>
|
---|
152 | <div class="example">
|
---|
153 | <pre class="example">"Foo!"
|
---|
154 | </pre></div>
|
---|
155 |
|
---|
156 | <p>Be careful to avoid passing a string constant to a function that
|
---|
157 | modifies the string it receives. The memory where the string constant
|
---|
158 | is stored may be read-only, which would cause a fatal <code>SIGSEGV</code>
|
---|
159 | signal that normally terminates the function (see <a href="Signals.html">Signals</a>. Even
|
---|
160 | worse, the memory may not be read-only. Then the function might
|
---|
161 | modify the string constant, thus spoiling the contents of other string
|
---|
162 | constants that are supposed to contain the same value and are unified
|
---|
163 | by the compiler.
|
---|
164 | </p>
|
---|
165 | <hr>
|
---|
166 | <div class="header">
|
---|
167 | <p>
|
---|
168 | Next: <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> [<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>
|
---|