Wednesday, May 23, 2012

do { } while(0) vs. if (1) { } in macros [closed]


Possible Duplicate:

Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?






When one needs to execute multiple statements within preprocessor macro, it's usually written like



#define X(a) do { f1(a); f2(a); } while(0)


so when this macro is used inside expressions like:



if (...)
X(a);


it would not be messed up.



The question is: wherever I've seen such expression, it's always do { ... } while(0);. Is there any reason to prefer such notion over (in my opinion more clear one) if (1) { ... }? Or am I wrong in my observations and they are equally popular?





No comments:

Post a Comment