⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multistmt.html

📁 this is a mirrored site c-faq. thought might need offline
💻 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/multistmt.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:53 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 10.4</title><link href="swapmacro.html" rev=precedes><link href="typedefvsdefine.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="swapmacro.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="typedefvsdefine.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a>&nbsp;<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>&middot;</font><!-- qtag -->Question 10.4</h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What's the best way to write a multi-statement macro?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The usual goal is tobe able toinvoke the macro as if it werean expression statement consisting of a function call:<pre>	MACRO(arg1, arg2);</pre>This means that the ``caller'' will be supplying the final semicolon,so the macro body should not.The macro body cannot thereforebe a simplebrace-enclosedcompound statement,becauseof thepossibilitythat the macro could be usedas the if branch of an if/else statement with an explicit elseclause:<pre>	if(cond)		MACRO(arg1, arg2);	else	/* some other code */</pre>Ifthe macroexpandedtoa simplecompound statement,the final,caller-suppliedsemicolonwould be a syntax error:<pre>	if(cond)		{stmt1; stmt2;};	else	/* some other code */</pre></p><p>The traditional solution,therefore, is to use<pre>	#define MACRO(arg1, arg2) do {	\		/* declarations */	\		stmt1;			\		stmt2;			\		/* ... */		\		} while(0)	/* (no trailing ; ) */</pre>When the caller appends a semicolon, this expansionbecomes a single statement regardless of context.(An optimizing compiler will remove any ``dead'' tests orbranches on the constant condition 0, although <TT>lint</TT> may complain.)</p><p>(Another possibility might be<pre>	#define MACRO(arg1, arg2) if(1) { \		stmt1; \		stmt2; \		} else</pre>but itis inferior,sinceit quietlybreaks the surrounding codeifthe caller happens to forget to appendthe semicolon upon invocation.)</p><p>If all of the statements in the intended macro are simple expressions,with no declarationsor loops,another technique is towrite a single, parenthesized expressionusing one or more comma operators:<pre>	#define FUNC(arg1, arg2) (expr1, expr2, expr3)</pre>(For anexample,see thefirst <TT>DEBUG()</TT>macro in question<a href="varargs.html">10.26</a>.)This technique also allows a value(in this case,<TT>expr3</TT>)to be ``returned.''</p><p>Some compilers,e.g. <TT>gcc</TT>,are also able to expand compact functions in-line,either automatically or at the programmer's request(perhaps with a nonstandard``<TT>inline</TT>'' keywordor other extension).</p><p>References:H&amp;S Sec. 3.3.2 p. 45<br>CT&amp;P Sec. 6.3 pp. 82-3<br></p><!-- aend --><p><hr><a href="swapmacro.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="typedefvsdefine.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a>&nbsp;<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>&nbsp;<a href="../eskimo.html">about eskimo</a>&nbsp;<a href="../search.html">search</a>&nbsp;<a href="../feedback.html">feedback</a>&nbsp;<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/multistmt.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:53 GMT --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -