Common practice in C:
#define FOO() do { } while (0)
While this is great, it is also possible to do:
#define FOO() { }((void)0)
{...}((void)0) has many advantages: you cannot accidentally combine logic, but at the end of the line is required ; , so odd expressions like this are not noticed: FOO() else {...} .
The only difference I noticed is that you need to use curly braces in if-statements.
if (a) FOO(); else BAR();
Must be written as:
if (a) { FOO(); } else { BAR(); }
At the same time, this quirk seems to work well, preventing the same problems that the do/while method is commonly used for.
Are there significant differences between the two methods?
In other words, if you see the code base using {...}((void)0) , are there practical reasons to switch to using do{..}while(0) , besides the fact that one difference is already noted?
c macros syntax
ideasman42
source share