1<html lang="en"> 2<head> 3<title>Swallowing the Semicolon - The C Preprocessor</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="The C Preprocessor"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Macro-Pitfalls.html#Macro-Pitfalls" title="Macro Pitfalls"> 9<link rel="prev" href="Operator-Precedence-Problems.html#Operator-Precedence-Problems" title="Operator Precedence Problems"> 10<link rel="next" href="Duplication-of-Side-Effects.html#Duplication-of-Side-Effects" title="Duplication of Side Effects"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 141997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 152008, 2009, 2010, 2011 16Free Software Foundation, Inc. 17 18Permission is granted to copy, distribute and/or modify this document 19under the terms of the GNU Free Documentation License, Version 1.3 or 20any later version published by the Free Software Foundation. A copy of 21the license is included in the 22section entitled ``GNU Free Documentation License''. 23 24This manual contains no Invariant Sections. The Front-Cover Texts are 25(a) (see below), and the Back-Cover Texts are (b) (see below). 26 27(a) The FSF's Front-Cover Text is: 28 29 A GNU Manual 30 31(b) The FSF's Back-Cover Text is: 32 33 You have freedom to copy and modify this GNU Manual, like GNU 34 software. Copies published by the Free Software Foundation raise 35 funds for GNU development. 36--> 37<meta http-equiv="Content-Style-Type" content="text/css"> 38<style type="text/css"><!-- 39 pre.display { font-family:inherit } 40 pre.format { font-family:inherit } 41 pre.smalldisplay { font-family:inherit; font-size:smaller } 42 pre.smallformat { font-family:inherit; font-size:smaller } 43 pre.smallexample { font-size:smaller } 44 pre.smalllisp { font-size:smaller } 45 span.sc { font-variant:small-caps } 46 span.roman { font-family:serif; font-weight:normal; } 47 span.sansserif { font-family:sans-serif; font-weight:normal; } 48--></style> 49<link rel="stylesheet" type="text/css" href="../cs.css"> 50</head> 51<body> 52<div class="node"> 53<a name="Swallowing-the-Semicolon"></a> 54<p> 55Next: <a rel="next" accesskey="n" href="Duplication-of-Side-Effects.html#Duplication-of-Side-Effects">Duplication of Side Effects</a>, 56Previous: <a rel="previous" accesskey="p" href="Operator-Precedence-Problems.html#Operator-Precedence-Problems">Operator Precedence Problems</a>, 57Up: <a rel="up" accesskey="u" href="Macro-Pitfalls.html#Macro-Pitfalls">Macro Pitfalls</a> 58<hr> 59</div> 60 61<h4 class="subsection">3.10.3 Swallowing the Semicolon</h4> 62 63<p><a name="index-semicolons-_0028after-macro-calls_0029-75"></a> 64Often it is desirable to define a macro that expands into a compound 65statement. Consider, for example, the following macro, that advances a 66pointer (the argument <code>p</code> says where to find it) across whitespace 67characters: 68 69<pre class="smallexample"> #define SKIP_SPACES(p, limit) \ 70 { char *lim = (limit); \ 71 while (p < lim) { \ 72 if (*p++ != ' ') { \ 73 p--; break; }}} 74</pre> 75 <p class="noindent">Here backslash-newline is used to split the macro definition, which must 76be a single logical line, so that it resembles the way such code would 77be laid out if not part of a macro definition. 78 79 <p>A call to this macro might be <code>SKIP_SPACES (p, lim)</code>. Strictly 80speaking, the call expands to a compound statement, which is a complete 81statement with no need for a semicolon to end it. However, since it 82looks like a function call, it minimizes confusion if you can use it 83like a function call, writing a semicolon afterward, as in 84<code>SKIP_SPACES (p, lim);</code> 85 86 <p>This can cause trouble before <code>else</code> statements, because the 87semicolon is actually a null statement. Suppose you write 88 89<pre class="smallexample"> if (*p != 0) 90 SKIP_SPACES (p, lim); 91 else ... 92</pre> 93 <p class="noindent">The presence of two statements—the compound statement and a null 94statement—in between the <code>if</code> condition and the <code>else</code> 95makes invalid C code. 96 97 <p>The definition of the macro <code>SKIP_SPACES</code> can be altered to solve 98this problem, using a <code>do ... while</code> statement. Here is how: 99 100<pre class="smallexample"> #define SKIP_SPACES(p, limit) \ 101 do { char *lim = (limit); \ 102 while (p < lim) { \ 103 if (*p++ != ' ') { \ 104 p--; break; }}} \ 105 while (0) 106</pre> 107 <p>Now <code>SKIP_SPACES (p, lim);</code> expands into 108 109<pre class="smallexample"> do {...} while (0); 110</pre> 111 <p class="noindent">which is one statement. The loop executes exactly once; most compilers 112generate no extra code for it. 113 114 </body></html> 115 116