admin管理员组文章数量:1278790
I have read in some other questions here why do { } while (0)
is good for function-like macros in C. Also, this form provides the break
instruction in order to simulate a return
statement.
But in those other questions, I never read anything "clear" about if (1) { } else
.
For me, this is almost equal to do { } while (0)
with the difference being that here we don't have the break
statement.
And I guess it’s better than if (1) { } else { }
because it allows us to use ;
.
Is it really "safe"? Are there any reasons for choosing this over do { } while (0)
?
I have read in some other questions here why do { } while (0)
is good for function-like macros in C. Also, this form provides the break
instruction in order to simulate a return
statement.
But in those other questions, I never read anything "clear" about if (1) { } else
.
For me, this is almost equal to do { } while (0)
with the difference being that here we don't have the break
statement.
And I guess it’s better than if (1) { } else { }
because it allows us to use ;
.
Is it really "safe"? Are there any reasons for choosing this over do { } while (0)
?
1 Answer
Reset to default 11Consider the alternatives you propose:
#define foo(x) do { /* main part of macro here */ } while (0)
#define foo(x) if (1) { /* main part of macro here */ } else
Both of these serve the purpose that the macro can be written like a function call:
foo(x);
Notably, the semicolon ends the statement, so the macro invocation looks like many other C statements. To somebody experienced in C, it looks odd if there is a line without a semicolon, like:
x = 3;
foo(x)
y = 4;
However, consider if somebody does accidentally write the above, perhaps because they knew foo
was a macro and thought it was a whole statement by itself. Then the while
form becomes:
x = 3;
do { /* main part of macro here */ } while (0)
y = 4;
This generates a compiler error since it is not valid C grammar (barring weird extensions). In contrast, the if
form becomes:
x = 3;
if (1) { /* main part of macro here */ } else
y = 4;
This may compile without an error, since the y = 4;
becomes part of the else
. So, in this respect, the while
form is superior since it catches an error that the if
form does not.
本文标签: Is 39if(1) else39 safe for functionlike macros in CStack Overflow
版权声明:本文标题:Is 'if(1) { } else' safe for function-like macros in C? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741298620a2370958.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
;
as you like these days. You can have;;;;;;;;;;
in your code and it compiles. – pmacfarlane Commented Feb 24 at 0:52if
statement and anelse
creates a syntax error. Otherwise, null statements are allowed anywhere in a function and always have been. – Jonathan Leffler Commented Feb 24 at 1:39do { … } while (0)
loop, ‘continue` behaves the same asbreak
. This doesn't affect the discussion. – Jonathan Leffler Commented Feb 24 at 1:51do { } while(0)
trick in macros that need to be widely portable. Otherwise in a quality (and/or MISRA C compliant portable) code base that enforces compound statements{}
after each and every if/else, then do-while(0) is bad style since it blocks diagnostics. What is do { } while(0) in macros and should we use it? – Lundin Commented Feb 24 at 8:36