Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>do ... while</code> and <code>if ... else</code> are there to make it so that a semicolon after your macro always means the same thing. Let's say you had something like your second macro.</p> <pre><code>#define BAR(X) f(x); g(x) </code></pre> <p>Now if you were to use <code>BAR(X);</code> in an <code>if ... else</code> statement, where the bodies of the if statement were not wrapped in curly brackets, you'd get a bad surprise.</p> <pre><code>if (corge) BAR(corge); else gralt(); </code></pre> <p>The above code would expand into</p> <pre><code>if (corge) f(corge); g(corge); else gralt(); </code></pre> <p>which is syntactically incorrect, as the else is no longer associated with the if. It doesn't help to wrap things in curly braces within the macro, because a semicolon after the braces is syntactically incorrect.</p> <pre><code>if (corge) {f(corge); g(corge);}; else gralt(); </code></pre> <p>There are two ways of fixing the problem. The first is to use a comma to sequence statements within the macro without robbing it of its ability to act like an expression.</p> <pre><code>#define BAR(X) f(X), g(X) </code></pre> <p>The above version of bar <code>BAR</code> expands the above code into what follows, which is syntactically correct.</p> <pre><code>if (corge) f(corge), g(corge); else gralt(); </code></pre> <p>This doesn't work if instead of <code>f(X)</code> you have a more complicated body of code that needs to go in its own block, say for example to declare local variables. In the most general case the solution is to use something like <code>do ... while</code> to cause the macro to be a single statement that takes a semicolon without confusion.</p> <pre><code>#define BAR(X) do { \ int i = f(X); \ if (i &gt; 4) g(i); \ } while (0) </code></pre> <p>You don't have to use <code>do ... while</code>, you could cook up something with <code>if ... else</code> as well, although when <code>if ... else</code> expands inside of an <code>if ... else</code> it leads to a "<a href="http://en.wikipedia.org/wiki/Dangling_else" rel="noreferrer">dangling else</a>", which could make an existing dangling else problem even harder to find, as in the following code.</p> <pre><code>if (corge) if (1) { f(corge); g(corge); } else; else gralt(); </code></pre> <p>The point is to use up the semicolon in contexts where a dangling semicolon is erroneous. Of course, it could (and probably should) be argued at this point that it would be better to declare <code>BAR</code> as an actual function, not a macro.</p> <p>In summary, the <code>do ... while</code> is there to work around the shortcomings of the C preprocessor. When those C style guides tell you to lay off the C preprocessor, this is the kind of thing they're worried about.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload