📄 faqcat204f.html
字号:
Rationale Sec. 3.8.3.3<br>H&S Sec. 3.3.9 p. 52<hr><hr><hr><a name="charize"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/charize.html"><!-- qtag -->Question 10.21</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I have an old macro<pre>#define CTRL(c) ('c' & 037)</pre>that doesn'tseem towork any more.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The intended use of this macrois in code like<pre> tchars.t_eofc = CTRL(D);</pre>which is expected to expand to<pre> tchars.t_eofc = ('D' & 037);</pre>based on the assumptionthatthe actual value ofthe parameter <TT>c</TT> will be substitutedeven inside the single quotes of a character constant.Preprocessing was never supposed to work this way;it was somewhat of an accidentthat a <TT>CTRL()</TT> macro like this ever worked.</p><p>ANSI C defines a new ``stringizing'' operator(see question <a href="faqcat7d4b.html?sec=ansi#stringize">11.17</a>),but there is no corresponding ``charizing'' operator.</p><p>The best solution to the problem is probably to move the single quotes from the definition to the invocation,by rewriting the macro as<pre> #define CTRL(c) ((c) & 037)</pre>and invoking it as<pre> CTRL('D')</pre>(Doing so also makes the macro ``syntactic'';see question <a href="faqcat204f.html?sec=cpp#slm">10.2</a>.)</p><p>It may also be possible to use the stringizing operatorand some indirection:<pre> #define CTRL(c) (*#c & 037)</pre>or<pre> #define CTRL(c) (#c[0] & 037)</pre>but neither of these would work as well as the original,since they wouldn't be valid in <TT>case</TT> labelsor as global variable initializers.(Global variable initializers and <TT>case</TT> labels require various flavors of constant expressions,and string literals and indirection aren't allowed.)</p><p>See also question <a href="faqcat7d4b.html?sec=ansi#macstrexp">11.18</a>.</p><p>References:ISO Sec. 6.8.3<br>H&S Secs. 7.11.2,7.11.3 pp. 226-7<hr><hr><hr><a name="macstrexp2"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/macstrexp2.html"><!-- qtag -->Question 10.22</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Why is the macro<pre> #define TRACE(n) printf("TRACE: %d\n", n)</pre>giving me the warning ``macro replacement within a string literal''?It seems to be expanding<pre> TRACE(count);</pre>as<pre> printf("TRACE: %d\count", count);</pre></p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat7d4b.html?sec=ansi#macstrexp">11.18</a>.<hr><hr><hr><a name="macstrexp1"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/macstrexp1.html"><!-- qtag -->Question 10.23</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can Iusea macro argumentinsidea stringliteralin the macro expansion?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat7d4b.html?sec=ansi#macstrexp">11.18</a>.<hr><hr><hr><a name="stringize"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/stringize.html"><!-- qtag -->Question 10.24</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm trying to usethe ANSI ``stringizing'' preprocessing operator`<TT>#</TT>'to insert the value of a symbolic constant into a message,but it keepsstringizing the macro's name rather than itsvalue.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat7d4b.html?sec=ansi#stringize">11.17</a>.<hr><hr><hr><a name="notgeneral"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/notgeneral.html"><!-- qtag -->Question 10.25</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I've got this tricky preprocessing I want to doandI can't figure out a wayto do it.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>C's preprocessoris not intended as a general-purpose tool.(Note also that it is not guaranteed to be availableas a separate program.)Rather than forcing it to do something inappropriate,you might want towriteyour own little special-purpose preprocessing tool, instead.You can easily get a utility likemake(1)to run it for you automatically.</p><p>If you are trying to preprocess something other than C,consider using a general-purpose preprocessor.(One older oneavailable on most Unix systemsis <TT>m4</TT>.)<hr><hr><hr><a name="varargs"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/varargs.html"><!-- qtag -->Question 10.26</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I write a macro which takes a variable number ofarguments,or use the preprocessor to ``turn off'' a function call with avariable number of arguments?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>One popular trick is todefine and invoke the macrowith a single, parenthesized ``argument''which in the macro expansion becomes the entire argument list,parentheses and all,for a function such as <TT>printf</TT>:<pre> #define DEBUG(args) (printf("DEBUG: "), printf args) if(n != 0) DEBUG(("n is %d\n", n));</pre>The obvious disadvantageis that the caller mustalways remember to use the extra parentheses.Another problem is thatthe macro expansion cannot insert any additional arguments(that is,<TT>DEBUG()</TT> couldn't expand tosomething like<TT>fprintf(debugfd, </TT>...<TT>)</TT>).</p><p><TT>gcc</TT> has an extensionwhich allows a function-like macro to accept a variable number of arguments,but it's not standard.Otherpossiblesolutions are:<UL><li>Usedifferent macros (<TT>DEBUG1</TT>, <TT>DEBUG2</TT>, etc.) depending onthe number of arguments.<li>Playgameswith commas:<pre> #define DEBUG(args) (printf("DEBUG: "), printf(args)) #define _ , DEBUG("i = %d" _ i);</pre><li>Play horrendous gameswith mismatched parentheses:<pre> #define DEBUG fprintf(stderr, DEBUG "%d", x);</pre></UL>(These all require care on the part of the user,and all of them are rather ugly.)</p><p>C99 introducesformal support forfunction-likemacros with variable-length argument lists.The notation <TT>...</TT> can appearat the end of the macro ``prototype''(just as it does for varargs functions),and the pseudomacro <TT>__VA_ARGS__</TT>in the macro definitionis replaced bythe variable arguments during invocation.</p><p>Finally,you can alwaysuse a bona-fide function, which cantake a variable numberof arguments in a well-defined way.See questions<a href="faqcat744e.html?sec=varargs#varargs1">15.4</a> and<a href="faqcat744e.html?sec=varargs#vprintf">15.5</a>.(If you needed a macro replacement,try using a function plus a non-function-like macro,e.g. <TT>#define printf myprintf</TT>.)</p><p>When you want to turn the debugging printouts off,you caneither use a different version of your debug macro:<pre> #define DEBUG(args) /* empty */</pre>or,if you're using real function calls,usestill morepreprocessor tricks to remove the functionname but not the arguments,suchas<pre> #define DEBUG (void)or<br> #define DEBUG if(1) {} else printfor<br> #define DEBUG 1 ? 0 : (void)</pre>(Thesetricks arepredicated on the assumption that a goodoptimizer will remove any ``dead'' <TT>printf</TT> calls ordegenerate cast-to-void parenthesized comma expressions.)See also question <a href="faqcat204f.html?sec=cpp#ifddef">10.14</a>.</p><p>Additional links:<a href="../../cpp/sd9.html" rel=subdocument>more ideas</a></p><p>References:C9X Sec. 6.8.3, Sec. 6.8.3.1<hr><hr><hr><a name="debugmacs"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../cpp/debugmacs.html"><!-- qtag -->Question 10.27</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I include expansions of the <TT>__FILE__</TT>and <TT>__LINE__</TT> macrosin a general-purpose debugging macro?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>This question tends to reduce to question<a href="faqcat204f.html?sec=cpp#varargs">10.26</a>.One solutioninvolves writing your debug macro in terms ofa varargs function(see questions<a href="faqcat744e.html?sec=varargs#varargs1">15.4</a> and<a href="faqcat744e.html?sec=varargs#vprintf">15.5</a>),and an auxiliary function which stashes the values of<TT>__FILE__</TT> and <TT>__LINE__</TT> away in static variables,as in:<pre>#include <stdio.h>#include <stdarg.h>void debug(const char *, ...);void dbginfo(int, const char *);#define DEBUG dbginfo(__LINE__, __FILE__), debugstatic const char *dbgfile;static int dbgline;void dbginfo(int line, const char *file){ dbgfile = file; dbgline = line;}void debug(const char *fmt, ...){ va_list argp; fprintf(stderr, "DEBUG: \"%s\", line %d: ", dbgfile, dbgline); va_start(argp, fmt); vfprintf(stderr, fmt, argp); va_end(argp); fprintf(stderr, "\n");}</pre>With this machinery in place,a call to<pre> DEBUG("i is %d", i);</pre>expands to<pre> dbginfo(__LINE__, __FILE__), debug("i is %d", i);</pre>and prints something like<pre> DEBUG: "x.c", line 10: i is 42</pre></p><p>A cunning improvementis the idea of havingthe stashing function return apointerto the bona-fide varargs function:<pre>void debug(const char *, ...);void (*dbginfo(int, const char *))(const char *, ...);#define DEBUG (*dbginfo(__LINE__, __FILE__))void (*dbginfo(int line, const char *file))(const char *, ...){ dbgfile = file; dbgline = line; return debug;}</pre>With these definitions,<pre> DEBUG("i is %d", i);</pre>gets expanded to<pre> (*dbginfo(__LINE__, __FILE__))("i is %d", i);</pre></p><p>Another,perhaps easier way might simply be to<pre> #define DEBUG printf("DEBUG: \"%s\", line %d: ", \ __FILE__,__LINE__),printf</pre>Now,<pre> DEBUG("i is %d", i);</pre>simply expands to<pre> printf("DEBUG: \"%s\", line %d: ", __FILE__,__LINE__),printf("i is %d", i);</pre></p><p>Finally, you may be able touse the<pre> #define _ ,</pre>trickfrom question <a href="faqcat204f.html?sec=cpp#varargs">10.26</a>.</p><p>Additional links:<a href="../../cpp/sd10.html" rel=subdocument>another idea</a><hr><hr><hr><hr><p>Read sequentially:<a href="faqcate034.html?sec=bool" rev=precedes>prev</a><a href="faqcat7d4b.html?sec=ansi" 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="../../cpp/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=cpp by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:03 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -