📄 varargs.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:46 2005 by faqproc version 2.7 --><!-- from source file cpp.sgml dated Wed Dec 21 13:52:14 2005 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/cpp/varargs.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:54 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 10.26</title><link href="notgeneral.html" rev=precedes><link href="debugmacs.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="notgeneral.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="debugmacs.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../index-2.html"><img src="../images/buttontop.gif" alt="top/contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><hr><p><!-- qbegin --><h1>comp.lang.c FAQ list<font color=blue>·</font><!-- qtag -->Question 10.26</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="../varargs/varargs1.html">15.4</a> and<a href="../varargs/vprintf.html">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="ifddef.html">10.14</a>.</p><p>Additional links:<a href="sd9.html" rel=subdocument>more ideas</a></p><p>References:C9X Sec. 6.8.3, Sec. 6.8.3.1<br></p><!-- aend --><p><hr><a href="notgeneral.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="debugmacs.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../questions.html"><img src="../images/buttontop.gif" alt="contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><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="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/cpp/varargs.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:54 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -