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

📄 swallowing-the-semicolon.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>The C Preprocessor</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="The C Preprocessor">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,

1997, 1998, 1999, 2000, 2001, 2002, 2003

Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.1 or

any later version published by the Free Software Foundation.  A copy of

the license is included in the

section entitled "GNU Free Documentation License".



   <p>This manual contains no Invariant Sections.  The Front-Cover Texts are

(a) (see below), and the Back-Cover Texts are (b) (see below).



   <p>(a) The FSF's Front-Cover Text is:



   <p>A GNU Manual



   <p>(b) The FSF's Back-Cover Text is:



   <p>You have freedom to copy and modify this GNU Manual, like GNU

     software.  Copies published by the Free Software Foundation raise

     funds for GNU development. 

-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Swallowing%20the%20Semicolon">Swallowing the Semicolon</a>,

Next:<a rel="next" accesskey="n" href="Duplication-of-Side-Effects.html#Duplication%20of%20Side%20Effects">Duplication of Side Effects</a>,

Previous:<a rel="previous" accesskey="p" href="Operator-Precedence-Problems.html#Operator%20Precedence%20Problems">Operator Precedence Problems</a>,

Up:<a rel="up" accesskey="u" href="Macro-Pitfalls.html#Macro%20Pitfalls">Macro Pitfalls</a>

<hr><br>

</div>



<h4 class="subsection">Swallowing the Semicolon</h4>



   <p>Often it is desirable to define a macro that expands into a compound

statement.  Consider, for example, the following macro, that advances a

pointer (the argument <code>p</code> says where to find it) across whitespace

characters:



<pre class="example">     #define SKIP_SPACES(p, limit)  \

     { char *lim = (limit);         \

       while (p &lt; lim) {            \

         if (*p++ != ' ') {         \

           p--; break; }}}

     </pre>



<p>Here backslash-newline is used to split the macro definition, which must

be a single logical line, so that it resembles the way such code would

be laid out if not part of a macro definition.



   <p>A call to this macro might be <code>SKIP_SPACES (p, lim)</code>.  Strictly

speaking, the call expands to a compound statement, which is a complete

statement with no need for a semicolon to end it.  However, since it

looks like a function call, it minimizes confusion if you can use it

like a function call, writing a semicolon afterward, as in

<code>SKIP_SPACES (p, lim);</code>



   <p>This can cause trouble before <code>else</code> statements, because the

semicolon is actually a null statement.  Suppose you write



<pre class="example">     if (*p != 0)

       SKIP_SPACES (p, lim);

     else ...

     </pre>



<p>The presence of two statements--the compound statement and a null

statement--in between the <code>if</code> condition and the <code>else</code>

makes invalid C code.



   <p>The definition of the macro <code>SKIP_SPACES</code> can be altered to solve

this problem, using a <code>do ... while</code> statement.  Here is how:



<pre class="example">     #define SKIP_SPACES(p, limit)     \

     do { char *lim = (limit);         \

          while (p &lt; lim) {            \

            if (*p++ != ' ') {         \

              p--; break; }}}          \

     while (0)

     </pre>



   <p>Now <code>SKIP_SPACES (p, lim);</code> expands into



<pre class="example">     do {...} while (0);

     </pre>



<p>which is one statement.  The loop executes exactly once; most compilers

generate no extra code for it.



   </body></html>



⌨️ 快捷键说明

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