Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Of course your macro can be independent of type specifier, this is one of the things that makes macros so powerful. You can either:</p> <ol> <li>Pass the variable name and the type and let the macro deal with the type.</li> <li>Pass the variable name <em>without</em> the type and assume that the macro performs legal operations on that variable, regardless of the type.</li> </ol> <p>Your actual problem is that in the first form you expect your macro to return an expression, but inside it you declare variables and what-not. I believe that because of that, you put the curly braces in the macro, and now it expands to: <code>({ bla bla })</code>, which would give you the following error message in ISO C99:</p> <blockquote> <p><code>ISO C forbids braced-groups within expressions</code></p> </blockquote> <p>Unfortunately, in ISO C99 you can't both have an expression AND declare temporary variables inside it. This is what functions are made for.</p> <p>So either use the second form that you described, that is pass the "result" variable to the macro:</p> <pre><code>#define min(type_, x_, y_, result_) \ do { \ type_ __tmp1 = (x_); \ type_ __tmp2 = (y_); \ result_ = (__tmp1 &lt; __tmp2 ? __tmp1 : __tmp2); \ } while(0) </code></pre> <p>or use an inline function instead.</p> <p>P.S:<br/> Also, <a href="https://stackoverflow.com/questions/154136/why-are-there-sometimes-meaningless-do-while-and-if-else-statements-in-c-c-mac">this</a> and <a href="https://stackoverflow.com/questions/1067226/c-multi-line-macro-do-while0-vs-scope-block">this</a> are worth reading for good practices when writing C macros.</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