source: public/doc/gnu-c/Strings.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: 6.3 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>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<!--
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="Strings"></span><div class="header">
59<p>
60Next: <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> &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="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>,
67terminated with the null character, the character with code zero.
68</p>
69<p>Programs often need to use strings with specific, fixed contents. To
70write one in a C program, use a <em>string constant</em> such as
71<code>&quot;Take me to your leader!&quot;</code>. The data type of a string constant
72is <code>char *</code>. For the full syntactic details of writing string
73constants, <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>
77for 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 &lsquo;<samp>text</samp>&rsquo; with six elements&mdash;five letters
84and the terminating null character. An equivalent way to get the same
85result is this,
86</p>
87<div class="example">
88<pre class="example">char text = &quot;Hello&quot;;
89</pre></div>
90
91<p>which copies the elements of the string constant, including <em>its</em>
92terminating 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
99plus the terminating null character.
100</p>
101<p>When you store a string into <code>message</code> be sure to check or prove
102that the length does not exceed its size. For example,
103</p>
104<div class="example">
105<pre class="example">void
106set_message (char *text)
107{
108 int i;
109 for (i = 0; i &lt; sizeof (message); i++)
110 {
111 message[i] = text[i];
112 if (text[i] == 0)
113 return;
114 }
115 fatal_error (&quot;Message is too long for `message');
116}
117</pre></div>
118
119<p>It&rsquo;s easy to do this with the standard library function
120<code>strncpy</code>, which fills out the whole destination array (up to a
121specified length) with null characters. Thus, if the last character
122of the destination is not null, the string did not fit. Many system
123libraries, including the GNU C library, hand-optimize <code>strncpy</code>
124to run faster than an explicit <code>for</code>-loop.
125</p>
126<p>Here&rsquo;s what the code looks like:
127</p>
128<div class="example">
129<pre class="example">void
130set_message (char *text)
131{
132 strncpy (message, text, sizeof (message));
133 if (message[sizeof (message) - 1] != 0)
134 fatal_error (&quot;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
139Library Reference Manual</cite>, for more information about the standard
140library functions for operating on strings.
141</p>
142<p>You can avoid putting a fixed length limit on strings you construct or
143operate on by allocating the space for them dynamically.
144See <a href="Dynamic-Memory-Allocation.html">Dynamic Memory Allocation</a>.
145</p>
146<hr>
147<div class="header">
148<p>
149Next: <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> &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>
150</div>
151
152
153
154</body>
155</html>
Note: See TracBrowser for help on using the repository browser.