📄 faqcat7d4b.html
字号:
only during preprocessor macro expansion.You cannot use them in normalsource code,butonly in macro definitions.</p><p>References:ISO Sec. 6.8.3.2, Sec. 6.8.3.5<hr><hr><hr><a name="macstrexp"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/macstrexp.html"><!-- qtag -->Question 11.18</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What does the message``warning: macro replacement within a string literal''mean?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Somepre-ANSI compilers/preprocessorsinterpreted macro definitions like<pre> #define TRACE(var, fmt) printf("TRACE: var = fmt\n", var)</pre>such that invocations like<pre> TRACE(i, %d);</pre>were expanded as<pre> printf("TRACE: i = %d\n", i);</pre>In other words, macro parameters wereexpandedeven insidestring literals and character constants.(This interpretation may even have been an accident of early implementations,but it can prove useful for macros like this.)</p><p>Macro expansion is <em>not</em> defined in this wayby K&R or by Standard C.(It can be dangerous and confusing:see question <a href="faqcat204f.html?sec=cpp#macstrexp2">10.22</a>.)When you do wantto turn macro arguments into strings,you can use the new <TT>#</TT> preprocessing operator,along with string literal concatenation(another new ANSI feature):<pre> #define TRACE(var, fmt) \ printf("TRACE: " #var " = " #fmt "\n", var)</pre>See also question <a href="faqcat7d4b.html?sec=ansi#stringize">11.17</a>.</p><p>References:H&S Sec. 3.3.8 p. 51<hr><hr><hr><a name="ifdefsyntax"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/ifdefsyntax.html"><!-- qtag -->Question 11.19</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm getting strange syntax errors insidelinesI've<TT>#ifdef</TT>fed out.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Under ANSI C,the text inside a ``turned off''<TT>#if</TT>, <TT>#ifdef</TT>, or <TT>#ifndef</TT>must still consist of ``valid preprocessingtokens.''This means thatthe characters <TT>"</TT> and <TT>'</TT> must each be paired just as inreal C code,and the pairs mustn't cross line boundaries.(Note particularly that an apostrophewithin a contracted wordlooks like the beginning of a character constant.)Therefore, natural-language commentsand pseudocodeshould always be writtenbetween the ``official'' comment delimiters <TT>/*</TT> and <TT>*/</TT>.(But see question<a href="faqcat38c2.html?sec=misc#nestcomment">20.20</a>,andalso<a href="faqcat204f.html?sec=cpp#notgeneral">10.25</a>.)</p><p>References:ISO Sec. 5.1.1.2, Sec. 6.1<br>H&S Sec. 3.2 p. 40<hr><hr><hr><a name="pragma"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/pragma.html"><!-- qtag -->Question 11.20</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What are <TT>#pragma</TT>s and what are they good for?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The <TT>#pragma</TT> directiveprovides a single, well-defined``escape hatch'' which can be used forall sorts of(nonportable)implementation-specific controls and extensions:source listing control,structure packing,warning suppression(like <TT>lint</TT>'s old <TT>/* NOTREACHED */</TT> comments),etc.</p><p>References:ISO Sec. 6.8.6<br>H&S Sec. 3.7 p. 61<hr><hr><hr><a name="pragmaonce"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/pragmaonce.html"><!-- qtag -->Question 11.21</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What does ``<TT>#pragma once</TT>'' mean?I found it in some header files.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It is an extension implemented by some preprocessors to help makeheader files idempotent;that is,to make sure thattheir contents are processed exactly onceeven if they are <TT>#include</TT>d multiple times.Itisequivalent to the<TT>#ifndef</TT> trick mentioned in question<a href="faqcat204f.html?sec=cpp#nestincl">10.7</a>,though less portable.Some people claim that <TT>#pragma once</TT> can be implemented``more efficiently''(of course only compilation efficiency is a factor here),but in fact a preprocessor that is seriousabout compilation efficiency can arrange for theportable<TT>#ifndef</TT> trick to be handled justas efficiently.<hr><hr><hr><a name="nonstrings"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/nonstrings.html"><!-- qtag -->Question 11.22</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Is <TT>char a[3] = "abc";</TT> legal?What does it mean?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It is legalin ANSI C(and perhaps in a few pre-ANSI systems),though useful only in rare circumstances.It declares an array of size three,initialized with the three characters <TT>'a'</TT>, <TT>'b'</TT>, and <TT>'c'</TT>,<em>without</em> the usual terminating <TT>'\0'</TT> character.The array is therefore not a true C stringand cannot be usedwith <TT>strcpy</TT>,<TT>printf</TT>'s <TT>%s</TT> format,etc.</p><p>Most of the time, you should let the compiler count the initializers when initializing arrays(in the case of the initializer <TT>"abc"</TT>, of course,the computed size will be 4).</p><p>References:ISO Sec. 6.5.7<br>H&S Sec. 4.6.4 p. 98<hr><hr><hr><a name="aryvsadr"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/aryvsadr.html"><!-- qtag -->Question 11.23</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Since array references decay into pointers,if <TT>arr</TT> is an array,what'sthe difference between<TT>arr</TT> and <TT>&arr</TT>?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcatca65.html?sec=aryptr#aryvsadr">6.12</a>.<hr><hr><hr><a name="voidparith"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/voidparith.html"><!-- qtag -->Question 11.24</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Why can't I perform arithmetic on a <TT>void *</TT> pointer?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The compiler doesn't know the size of the pointed-toobjects.(Remember that pointer arithmeticis always in terms ofthe pointed-to size;see also question <a href="faqcatabdc.html?sec=ptrs#explscale">4.4</a>.)Therefore,arithmetic on <TT>void *</TT>'s is disallowed(though some compilersallow it as an extension).Before performing arithmetic,convert the pointer either to <TT>char *</TT>or to thepointertype you're trying to manipulate(but see alsoquestions <a href="faqcatabdc.html?sec=ptrs#castincr">4.5</a> and <a href="faqcat5e04.html?sec=strangeprob#ptralign">16.7</a>).</p><p>References:ISO Sec. 6.1.2.5, Sec. 6.3.6<br>H&S Sec. 7.6.2 p. 204<hr><hr><hr><a name="memmove"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/memmove.html"><!-- qtag -->Question 11.25</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What's the difference between<TT>memcpy</TT> and<TT>memmove</TT>?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font><TT>memmove</TT> offers guaranteed behaviorif thememoryregionspointed to by thesource and destination argumentsoverlap.<TT>memcpy</TT> makes no such guarantee,and may therefore be more efficiently implementable.When in doubt, it's safer to use <TT>memmove</TT>.</p><p>It seemssimple enough to implement <TT>memmove</TT>;the overlap guaranteeapparently requiresonly an additional test:<pre>void *memmove(void *dest, void const *src, size_t n){ register char *dp = dest; register char const *sp = src; if(dp < sp) { while(n-- > 0) *dp++ = *sp++; } else { dp += n; sp += n; while(n-- > 0) *--dp = *--sp; } return dest;}</pre>The problemwith this codeis in that additional test:the comparison<TT>(dp < sp)</TT>is not quite portable(it compares two pointerswhich do not necessarily point within the same object)and may not be as cheap as it looks.On some machines(particularly segmented architectures),it may be tricky and significantly less efficient<a href="../../ansi/norml.html" rel=subdocument>[footnote]</a>to implement.</p><p>References:K&R2 Sec. B3 p. 250<br>ISO Sec. 7.11.2.1, Sec. 7.11.2.2<br>Rationale Sec. 4.11.2<br>H&S Sec. 14.3 pp. 341-2<br>PCS Sec. 11 pp. 165-6<hr><hr><hr><a name="malloc0"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/malloc0.html"><!-- qtag -->Question 11.26</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What should <TT>malloc(0)</TT> do?Return a null pointer or a pointer to 0 bytes?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The ANSI/ISO Standard says that it may do either;thebehavior is implementation-defined(see question <a href="faqcat7d4b.html?sec=ansi#undef">11.33</a>).Portable code must eithertake care not to call <TT>malloc(0)</TT>,or be prepared forthe possibility of a null return.</p><p>References:ISO Sec. 7.10.3<br>PCS Sec. 16.1 p. 386<hr><hr><hr><a name="extidsignif"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/extidsignif.html"><!-- qtag -->Question 11.27</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Why does the ANSI Standardplace limits onthe lengthand case-significanceofexternal identifiers?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The problem islinkerswhich areunder control ofneitherthe ANSI/ISO Standard northe C compiler developers on the systems which have them.The limitation is only that identifiers be<em>significant</em>in some initial sequence of characters,not that they be restricted tothat many charactersin totallength.(The limitation was to six charactersin the original ANSI Standard,but has been relaxed to31in C99.)</p><p>References:ISO Sec. 6.1.2, Sec. 6.9.1<br>Rationale Sec. 3.1.2<br>C9X Sec. 6.1.2<br>H&S Sec. 2.5 pp. 22-3<hr><hr><hr><a name="noalias"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/noalias.html"><!-- qtag -->Question 11.28</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What was <TT>noalias</TT> and what ever happened to it?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font><TT>noalias</TT> was another type qualifier,in the same syntactic class as <TT>const</TT> and <TT>volatile</TT>,which was intended to assert thatanobjectwas notpointed to (``aliased'') by other pointers.The primary application,which is an important one,would have been for the formal parameters of functionsdesigned to perform computations on large arrays.A compiler cannot usually take advantage of vectorizationor other parallelizationhardware(on supercomputers which have it)unless it can ensurethat the source and destination arrays do not overlap.</p><p>The <TT>noalias</TT> keyword was not backed up by any ``prior art,''and it was introduced late in the review and approval process.It wassurprisinglydifficult to define precisely and explain coherently,and sparked widespread, acrimonious debate,including a scathing pan by Dennis Ritchie.It had far-ranging implications,particularly for several standard library interfaces,for which easy fixes were not readily apparent.</p><p>Because of the criticism and the difficulty of defining<TT>noalias</TT> well,the Committeedeclined to adopt it,in spite of its superficialattractions.(When writing a standard,features cannot be introduced halfway;their full integration,and all implications,must be understood.)The need for an explicit mechanismto support parallel implementation ofnon-overlapping operations remainsunfilled(althoughsome work is being doneonthe problem).</p><p>References:ISO Sec. 6.9.6<hr><hr><hr><a name="preansi"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../ansi/preansi.html"><!-- qtag -->Question 11.29a</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>My compiler is rejecting the simplest possible test programs,with all kinds of syntax errors.It's complaining about thefirst line of<pre> main(int argc, char **argv) { return 0; }</pre></p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Perhaps it is a pre-ANSI compiler,unable to accept function prototypes and the like.</p><p>See also questions<a href="faqcatd3c2.html?sec=decl#autoaggrinit">1.31</a>,<a href="faqcat204f.html?sec=cpp#headerglom">10.9</a>,<a href="faqcat7d4b.html?sec=ansi#preansilib">11.30</a>,and<a href="faqcat5e04.html?sec=strangeprob#unclosed">16.1b</a>.</p><p>If you don't have access to an ANSI compiler,and you need to convert some newer code(such as that in this list)so that you can compile it,perform these steps:</p><OL><li>Removethe argument type information fromfunction prototype declarations,and convert prototype-style function definitions to old style.The new-style declarations<pre> extern int f1(void); extern int f2(int); int main(int argc, char **argv) { ... } int f3(void) { ... }</pre>would berewritten as
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -