📄 faqcat204f.html
字号:
(that is, part of)the program you're trying to compile,your search will obviously beginat the spotwhere you found the program.(Again, see question <a href="faqcatccbd.html?sec=resources#sources">18.16</a>.)</p><p>In general, however, asking if someone can``send you a copy''of a missing header file is not likely to be productive.Standard headers exist in part so that definitions appropriate to yourcompiler,operating system,andprocessorcan be supplied.You cannotjustpick up a copy of someone else's header fileand expect it to work,unless that person is using exactly the same environment.Nonstandard headers--such asthose specific toa particular operating systemor third-party library--aren'tgenerally any more portable;the OS-specific ones are likely to be <em>very</em> specificto a particular version and release of the OS,and the third-party ones are likely to be equally tiedto a particular version of the library.</p><p>The bottom line is thata random person on the netis unlikely to be able to send you a working copyof the header file you(seem to)need.You may actually have aportability problem(see section <a href="faqcatea63.html?sec=osdep#index">19</a>),or a compiler problem(in which case you might be able toask your compiler vendor why the file was not provided,or to send a replacement copy).Otherwise(if the header is third-party or application-specific),see question <a href="faqcatccbd.html?sec=resources#sources">18.16</a>.<hr><hr><hr><a name="ifstrcmp"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/ifstrcmp.html"><!-- qtag -->Question 10.12</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I construct preprocessor <TT>#if</TT> expressionswhich compare strings?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>You can't do it directly;preprocessor <TT>#if</TT> arithmetic uses onlyintegers.An alternative is to<TT>#define</TT>severalmacros with symbolic names and distinctintegervalues,and implement conditionalson those:<pre> #define RED 1 #define BLUE 2 #define GREEN 3 #if COLOR == RED /* red case */ #else #if COLOR == BLUE /* blue case */ #else #if COLOR == GREEN /* green case */ #else /* default case */ #endif #endif #endif</pre>(Standard Cspecifies a new <TT>#elif</TT> directivewhich makes if/else chains like these a bit cleaner.)</p><p>See also question <a href="faqcat38c2.html?sec=misc#strswitch">20.17</a>.</p><p>References:K&R2 Sec. 4.11.3 p. 91<br>ISO Sec. 6.8.1<br>H&S Sec. 7.11.1 p. 225<hr><hr><hr><a name="ifexpr"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/ifexpr.html"><!-- qtag -->Question 10.13</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Does the <TT>sizeof</TT> operator work in preprocessor <TT>#if</TT> directives?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>No.Preprocessing happens during an earlierphase of compilation,before type names have been parsed.Insteadof <TT>sizeof</TT>,considerusing the predefined constants in ANSI's <TT><limits.h></TT>,if applicable,orperhapsa ``configure'' script.(Better yet,try to write code which is inherently insensitive to type sizes;see alsoquestions <a href="faqcatd3c2.html?sec=decl#inttypes">1.1</a> and <a href="faqcatd3c2.html?sec=decl#int16">1.3</a>.)</p><p>Additional links:<a href="../../cpp/ifsizeof.ct.html">nice explanation by Chris Torek</a></p><p>References:ISO Sec. 5.1.1.2, Sec. 6.8.1<br>H&S Sec. 7.11.1 p. 225<hr><hr><hr><a name="ifddef"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/ifddef.html"><!-- qtag -->Question 10.14</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Can I use an <TT>#ifdef</TT> in a <TT>#define</TT> line,to define something two differentways,like this?<pre> #define a b \ #ifdef whatever c d #else e f g #endif</pre></p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>No.You can't``run the preprocessor on itself,''so to speak.What you can do isuse one of twocompletely separate<TT>#define</TT> lines,depending onthe<TT>#ifdef</TT> setting:<pre> #ifdef whatever #define a b c d #else #define a b e f g #endif</pre></p><p>References:ISO Sec. 6.8.3, Sec. 6.8.3.4<br>H&S Sec. 3.2 pp. 40-1<hr><hr><hr><a name="iftypedef"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/iftypedef.html"><!-- qtag -->Question 10.15</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Is there anything likean <TT>#ifdef</TT> for <TT>typedef</TT>s?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Unfortunately, no.(There can't be,because types and <TT>typedef</TT>shaven't been parsed at preprocessing time.)You may have to keepsets of preprocessor macros(e.g. <TT>MY_TYPE_DEFINED</TT>)recording whethercertaintypedefs have been declared.</p><p>Seealsoquestions <a href="faqcatd3c2.html?sec=decl#typedefvsdefine">1.13</a> and <a href="faqcat204f.html?sec=cpp#ifexpr">10.13</a>.</p><p>References:ISO Sec. 5.1.1.2, Sec. 6.8.1<br>H&S Sec. 7.11.1 p. 225<hr><hr><hr><a name="ifendian"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/ifendian.html"><!-- qtag -->Question 10.16</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I use a preprocessor <TT>#if</TT> expression totellwhether a machine's byte orderis big-endian or little-endian?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>You probably can't.Theusual techniques for detecting endiannessinvolve pointers or arrays of <TT>char</TT>,or maybe unions,butpreprocessorarithmetic uses only long integers,andthere is no concept ofaddressing.Another tempting possibility is something like<pre> #if 'ABCD' == 0x41424344</pre>but this isn't reliable, either.At any rate,the integer formats used in preprocessor <TT>#if</TT> expressions are not necessarilythe same asthose that will be used at run time.</p><p>Are yousure you need to know the machine's endiannessexplicitly?Usually it's better to write code which doesn't care(see for example the code fragment in question <a href="faqcat1d60.html?sec=stdio#extconform">12.42</a>).See also question <a href="faqcat38c2.html?sec=misc#endiantest">20.9</a>.</p><p>References:ISO Sec. 6.8.1<br>H&S Sec. 7.11.1 p. 225<hr><hr><hr><a name="ifdefsyntax"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/ifdefsyntax.html"><!-- qtag -->Question 10.17</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>See question <a href="faqcat7d4b.html?sec=ansi#ifdefsyntax">11.19</a>.<hr><hr><hr><a name="unifdef"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/unifdef.html"><!-- qtag -->Question 10.18</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I inherited some codewhich contains far too many <TT>#ifdef</TT>'s for my taste.How can I preprocessthecodetoleave only one conditional compilation set,without running it through the preprocessorand expanding all of the <TT>#include</TT>'s and <TT>#define</TT>'s as well?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>There are programsfloating aroundcalled <TT>unifdef</TT>,<TT>rmifdef</TT>,and<TT>scpp</TT>(``selective C preprocessor'')which do exactly this.See question<a href="faqcatccbd.html?sec=resources#sources">18.16</a>.<hr><hr><hr><a name="listpredef"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/listpredef.html"><!-- qtag -->Question 10.19</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I list all of the predefined identifiers?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>There's no standard way, although it is a common need.<TT>gcc</TT> provides a <TT>-dM</TT> option which works with <TT>-E</TT>,and other compilers may provide something similar.If the compiler documentationis unhelpful,themost expedient way is probably to extractprintable stringsfrom the compiler or preprocessor executablewith something like theUnix<TT>strings</TT> utility.Beware that many traditionalsystem-specificpredefined identifiers(e.g. ``<TT>unix</TT>'')are non-Standard(because they clash with the user's namespace)and are being removed or renamed.(In any case,as a general rule,it'sconsidered wise to keep conditional compilation to a minimum.)</p><p>Additional links:<a href="../../cpp/sd8.html" rel=subdocument>sources!</a><hr><hr><hr><a name="oldpaste"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/oldpaste.html"><!-- qtag -->Question 10.20</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I have some old code that tries to construct identifiers with amacro like<pre>#define Paste(a, b) a/**/b</pre>but it doesn't work any more.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Itwas an undocumented feature of some earlypreprocessor implementations(notablyReiser's)that commentsdisappeared entirely and could therefore be usedfor token pasting.ANSI affirms(as did K&R1)that comments are replaced with white space,so they cannot portably be used in a <TT>Paste()</TT> macro.However, since the need for pasting tokens wasdemonstrated and real,ANSIintroduced a well-defined token-pasting operator, <TT>##</TT>,which can be used like this:<pre> #define Paste(a, b) a##b</pre></p><p>Here is one othermethod you could try forpasting tokens undera pre-ANSI compiler:<pre> #define XPaste(s) s #define Paste(a, b) XPaste(a)b</pre>See also question<a href="faqcat7d4b.html?sec=ansi#stringize">11.17</a>.</p><p>References:ISO Sec. 6.8.3.3<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -