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>Strings (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Strings (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Strings (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="Arrays.html" rel="up" title="Arrays">
|
---|
33 | <link href="Array-Type-Designators.html" rel="next" title="Array Type Designators">
|
---|
34 | <link href="Declaring-an-Array.html" rel="prev" title="Declaring an Array">
|
---|
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="Strings"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Array-Type-Designators.html" accesskey="n" rel="next">Array Type Designators</a>, Previous: <a href="Declaring-an-Array.html" accesskey="p" rel="prev">Declaring an Array</a>, Up: <a href="Arrays.html" accesskey="u" rel="up">Arrays</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="Strings-1"></span><h3 class="section">16.3 Strings</h3>
|
---|
64 | <span id="index-string"></span>
|
---|
65 |
|
---|
66 | <p>A string in C is a sequence of elements of type <code>char</code>,
|
---|
67 | terminated with the null character, the character with code zero.
|
---|
68 | </p>
|
---|
69 | <p>Programs often need to use strings with specific, fixed contents. To
|
---|
70 | write one in a C program, use a <em>string constant</em> such as
|
---|
71 | <code>"Take me to your leader!"</code>. The data type of a string constant
|
---|
72 | is <code>char *</code>. For the full syntactic details of writing string
|
---|
73 | constants, <a href="String-Constants.html">String Constants</a>.
|
---|
74 | </p>
|
---|
75 | <p>To declare a place to store a non-constant string, declare an array of
|
---|
76 | <code>char</code>. Keep in mind that it must include one extra <code>char</code>
|
---|
77 | for the terminating null. For instance,
|
---|
78 | </p>
|
---|
79 | <div class="example">
|
---|
80 | <pre class="example">char text = { 'H', 'e', 'l', 'l', 'o', 0 };
|
---|
81 | </pre></div>
|
---|
82 |
|
---|
83 | <p>declares an array named ‘<samp>text</samp>’ with six elements—five letters
|
---|
84 | and the terminating null character. An equivalent way to get the same
|
---|
85 | result is this,
|
---|
86 | </p>
|
---|
87 | <div class="example">
|
---|
88 | <pre class="example">char text = "Hello";
|
---|
89 | </pre></div>
|
---|
90 |
|
---|
91 | <p>which copies the elements of the string constant, including <em>its</em>
|
---|
92 | terminating null character.
|
---|
93 | </p>
|
---|
94 | <div class="example">
|
---|
95 | <pre class="example">char message[200];
|
---|
96 | </pre></div>
|
---|
97 |
|
---|
98 | <p>declares an array long enough to hold a string of 199 ASCII characters
|
---|
99 | plus the terminating null character.
|
---|
100 | </p>
|
---|
101 | <p>When you store a string into <code>message</code> be sure to check or prove
|
---|
102 | that the length does not exceed its size. For example,
|
---|
103 | </p>
|
---|
104 | <div class="example">
|
---|
105 | <pre class="example">void
|
---|
106 | set_message (char *text)
|
---|
107 | {
|
---|
108 | int i;
|
---|
109 | for (i = 0; i < sizeof (message); i++)
|
---|
110 | {
|
---|
111 | message[i] = text[i];
|
---|
112 | if (text[i] == 0)
|
---|
113 | return;
|
---|
114 | }
|
---|
115 | fatal_error ("Message is too long for `message');
|
---|
116 | }
|
---|
117 | </pre></div>
|
---|
118 |
|
---|
119 | <p>It’s easy to do this with the standard library function
|
---|
120 | <code>strncpy</code>, which fills out the whole destination array (up to a
|
---|
121 | specified length) with null characters. Thus, if the last character
|
---|
122 | of the destination is not null, the string did not fit. Many system
|
---|
123 | libraries, including the GNU C library, hand-optimize <code>strncpy</code>
|
---|
124 | to run faster than an explicit <code>for</code>-loop.
|
---|
125 | </p>
|
---|
126 | <p>Here’s what the code looks like:
|
---|
127 | </p>
|
---|
128 | <div class="example">
|
---|
129 | <pre class="example">void
|
---|
130 | set_message (char *text)
|
---|
131 | {
|
---|
132 | strncpy (message, text, sizeof (message));
|
---|
133 | if (message[sizeof (message) - 1] != 0)
|
---|
134 | fatal_error ("Message is too long for `message');
|
---|
135 | }
|
---|
136 | </pre></div>
|
---|
137 |
|
---|
138 | <p>See <a href="https://www.gnu.org/software/libc/manual/html_node/String-and-Array-Utilities.html#String-and-Array-Utilities">The GNU C Library</a> in <cite>The GNU C
|
---|
139 | Library Reference Manual</cite>, for more information about the standard
|
---|
140 | library functions for operating on strings.
|
---|
141 | </p>
|
---|
142 | <p>You can avoid putting a fixed length limit on strings you construct or
|
---|
143 | operate on by allocating the space for them dynamically.
|
---|
144 | See <a href="Dynamic-Memory-Allocation.html">Dynamic Memory Allocation</a>.
|
---|
145 | </p>
|
---|
146 | <hr>
|
---|
147 | <div class="header">
|
---|
148 | <p>
|
---|
149 | Next: <a href="Array-Type-Designators.html" accesskey="n" rel="next">Array Type Designators</a>, Previous: <a href="Declaring-an-Array.html" accesskey="p" rel="prev">Declaring an Array</a>, Up: <a href="Arrays.html" accesskey="u" rel="up">Arrays</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>
|
---|
150 | </div>
|
---|
151 |
|
---|
152 |
|
---|
153 |
|
---|
154 | </body>
|
---|
155 | </html>
|
---|