📄 faqcate107.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:45 2005 by faqproc version 2.7 --><!-- from source file charstring.sgml dated Sat Feb 7 18:22:16 2004 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=charstring by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:57:56 GMT --><head><base ><meta name=GENERATOR content="faqproc"><title>Characters and Strings</title></head><body bgcolor="#ffffff"><H1>8. Characters and Strings</H1><a name="strvschar"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../charstring/strvschar.html"><!-- qtag -->Question 8.1</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Why doesn't<pre>strcat(string, '!');</pre>work?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>There is a very realdifferencebetween characters and strings,and<TT>strcat</TT> concatenates<em>strings</em>.</p><p>Acharacter constantlike<TT>'!'</TT> represents a single character.A string literal between double quotesusually represents multiple characters.A string literal like <TT>"!"</TT> seems to represent a single character,but it actually contains two:the <TT>!</TT> you requested,and the <TT>\0</TT> which terminates all strings in C.</p><p>Characters in C are represented by small integers corresponding to their character set values(see also question <a href="faqcate107.html?sec=charstring#asciivals">8.6</a>).Strings are represented by arrays of characters;you usually manipulate a pointerto the first character of the array.It is never correct to use one when the other is expected.Toappend a <TT>!</TT> to a string,use<pre> strcat(string, "!");</pre></p><p>See also questions<a href="faqcatd3c2.html?sec=decl#strlitinit">1.32</a>,<a href="faqcatbafd.html?sec=malloc#malloc2">7.2</a>,and<a href="faqcat5e04.html?sec=strangeprob#strlitnomod">16.6</a>.</p><p>References:CT&P Sec. 1.5 pp. 9-10<hr><hr><hr><a name="stringeq"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../charstring/stringeq.html"><!-- qtag -->Question 8.2</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm checking a string to see if it matches a particular value.Why isn't this code working?<pre> char *string; ... if(string == "value") { /* string matches "value" */ ... }</pre></p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Strings in C are represented as arrays of characters,and C never manipulates(assigns, compares, etc.)arrays as a whole.<a href="../../charstring/fn44.html" rel=subdocument>[footnote]</a>The <TT>==</TT> operator in the code fragment above compares twopointers--thevalue of the pointer variable <TT>string</TT>and a pointer to the string literal<TT>"value"</TT>--tosee if they are equal,that is,if they point to the same place.They probably don't,so the comparison never succeeds.</p><p>To compare two strings,you generallyusethe library function<TT>strcmp</TT>:<pre> if(strcmp(string, "value") == 0) { /* string matches "value" */ ... }</pre><hr><hr><hr><a name="assign"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../charstring/assign.html"><!-- qtag -->Question 8.3</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>If I can say<pre> char a[] = "Hello, world!";</pre>why can't I say<pre> char a[14]; a = "Hello, world!";</pre></p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Strings are arrays, and you can't assign arrays directly.Use <TT>strcpy</TT> instead:<pre> strcpy(a, "Hello, world!");</pre></p><p>See alsoquestions <a href="faqcatd3c2.html?sec=decl#strlitinit">1.32</a>, <a href="faqcatabdc.html?sec=ptrs#mimic">4.2</a>, <a href="faqcatca65.html?sec=aryptr#arrayassign">6.5</a>, and <a href="faqcatbafd.html?sec=malloc#malloc2">7.2</a>.<hr><hr><hr><a name="malloc"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../charstring/malloc.html"><!-- qtag -->Question 8.4</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I can't get <TT>strcat</TT> to work.I tried<pre> char *s1 = "Hello, "; char *s2 = "world!"; char *s3 = strcat(s1, s2);</pre>but I got strange results.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcatbafd.html?sec=malloc#malloc2">7.2</a>.<hr><hr><hr><a name="strlitinit"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../charstring/strlitinit.html"><!-- qtag -->Question 8.5</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What is the difference betweenthese initializations?<pre>char a[] = "string literal";char *p = "string literal";</pre>My programcrashes ifI try toassign a new value to <TT>p[i]</TT>.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcatd3c2.html?sec=decl#strlitinit">1.32</a>.<hr><hr><hr><a name="asciivals"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../charstring/asciivals.html"><!-- qtag -->Question 8.6</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I get thenumericvalue(i.e. ASCII or other character set code)corresponding to acharacter,or vice versa?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>In C,characters are represented by small integerscorresponding to their valuesin the machine's character set.Therefore,you don't need a conversionfunction:if you have the character,you have its value.The following fragment:<pre> int c1 = 'A', c2 = 65; printf("%c %d %c %d\n", c1, c1, c2, c2);</pre>prints<pre> A 65 A 65</pre>on an ASCII machine.</p><p>To convert back and forth between the digit characters and thecorresponding integers in the range 0-9,add or subtract the constant <TT>'0'</TT>(that is, the character value <TT>'0'</TT>).</p><p>See also questions<a href="faqcate107.html?sec=charstring#sizeofchar">8.9</a>,<a href="faqcat1067.html?sec=lib#itoa">13.1</a>,and<a href="faqcat38c2.html?sec=misc#hexio">20.10</a>.<hr><hr><hr><a name="substr"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../charstring/substr.html"><!-- qtag -->Question 8.7</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Does C have anything like the ``substr''(extract substring)routine present in other languages?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat1067.html?sec=lib#substr">13.3</a>.<hr><hr><hr><a name="runtimesc"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../charstring/runtimesc.html"><!-- qtag -->Question 8.8</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm readingstringstyped bythe userinto an array,and then printing them out later.When the user typesa sequence like <TT>\n</TT>,whyisn't it beinghandled properly?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Character sequences like <TT>\n</TT>are interpreted at compile time.Whena backslash and an adjacent <TT>n</TT>appearin a character constant or string literal,theyare translated immediately into a single newline character.(Analogous translations occur,of course,for the other character escape sequences.)When you're reading strings from the user or a file,however,no interpretation like this is performed:a backslashis readand printedjust like any other character, with no particular interpretation.</p><p>(Some interpretation of the newline character may be done during run-time I/O,but for a completely different reason;see question <a href="faqcat1d60.html?sec=stdio#textvsbinary">12.40</a>.)</p><p>See also question <a href="faqcat1d60.html?sec=stdio#printfpercent">12.6</a>.</p><p>Additional links:<a href="faqcat1d60.html?sec=stdio#cescapes">further reading</a><hr><hr><hr><a name="sizeofchar"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../charstring/sizeofchar.html"><!-- qtag -->Question 8.9</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I think something's wrong with my compiler:I just noticed that <TT>sizeof('a')</TT> is 2, not 1(i.e. not <TT>sizeof(char)</TT>).</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Perhaps surprisingly,characterconstants in Care of type <TT>int</TT>,so <TT>sizeof('a')</TT> is <TT>sizeof(int)</TT>(thoughthis is another areawhere C++ differs).See alsoquestion <a href="faqcatbafd.html?sec=malloc#sizeofchar">7.8</a>,and<a href="../../charstring/sd5.html" rel=subdocument>this footnote</a>.</p><p>References:ISO Sec. 6.1.3.4<br>H&S Sec. 2.7.3 p. 29<hr><hr><hr><a name="wchar"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../charstring/wchar.html"><!-- qtag -->Question 8.10</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm starting to think about multinational character sets,and I'm worried about the implicationsof making <TT>sizeof(char)</TT> be 2so that 16-bit character sets can be represented.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>If type <TT>char</TT> were made 16 bits,<TT>sizeof(char)</TT> would still be 1,and <TT>CHAR_BIT</TT> in <TT><limits.h></TT> would be 16,and it would simply be impossible to declare(or allocate with <TT>malloc</TT>)a single 8-bit object.</p><p>Traditionally, a byte is not necessarily 8 bits, butmerely a smallish region of memory,usuallysuitablefor storing one character.The C Standardfollows this usage, sothe bytes used by <TT>malloc</TT>and <TT>sizeof</TT> can be more than 8 bits.<a href="../../charstring/octet.html" rel=subdocument>[footnote]</a>(The Standard does not allow them to be less.)</p><p>To allow manipulation of multinational character setswithout requiringan expansion of type <TT>char</TT>,ANSI/ISO C defines the ``wide'' character type <TT>wchar_t</TT>,and corresponding widestring literals,andfunctionsfor manipulatingand convertingstrings of wide characters.</p><p>See also question <a href="faqcatbafd.html?sec=malloc#sizeofchar">7.8</a>.</p><p>References:ISO Sec. 5.2.1.2, Sec. 6.1.3.4, Sec. 6.1.4, Sec. 7.1.6, Sec. 7.10.7, Sec. 7.10.8<br>Rationale Sec. 2.2.1.2<br>H&S Sec. 2.7.3 pp. 29-30, Sec. 2.7.4 p. 33, Sec. 11.1 p. 293, Secs. 11.7,11.8 pp. 303-310<hr><hr><hr><hr><p>Read sequentially:<a href="faqcatbafd.html?sec=malloc" rev=precedes>prev</a><a href="faqcate034.html?sec=bool" rel=precedes>next</a><a href="faqcat.html" rev=subdocument>up</a></p><hr><p><br><!-- lastfooter --><a href="../../about.html">about this FAQ list</a> <a href="../../eskimo.html">about eskimo</a> <a href="../../search.html">search</a> <a href="../../feedback.html">feedback</a> <a href="../../charstring/copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></body><!-- Mirrored from c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=charstring by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:57:58 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -