Possible Duplicate:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
Why is C source code I run across sometimes wrapped with a do...while(0) loop?
do {
parser->http_errno = e;
parser->error_lineno = __LINE__;
} while (0)
Why use that, versus this:
parser->http_errno = e;
parser->error_lineno = __LINE__;
I suspect this has something to do with thread safety, but I’m not sure.
It’s done so that you can use it inside a macro, while requiring the macro user to use a semicolon at the end, just like a regular statement.
In other words, whenever you want multiple statements inside a function-like macro
FOO(...), you wrap it withdo { ... } while (0)so that the user of the macro can (and, in fact, must) call it asFOO(x);, instead of simplyFOO(x)(whose lack of semicolon can be weird for both humans and computers).