On Mon, 10 Mar 2008, William "Chops" Westfield wrote: > > On Mar 10, 2008, at 7:03 PM, sergio masci wrote: > > > Could you please explain what you meen by "making a multi-line macro > > safe". I have never seen this contruct used to protect code. > > A macro that contains multiple lines of code is not "safe" to use in > assorted places, for example, to avoid compiler warnings, I convert > all my old > macro definitions: > #define CHECK_ERR if (errcode = get_error()) {print_error(errcode)} > to #define CHECK_ERR errcode = get_error(); if (errcode) {print_error > (errcode)} > > that's all very nice, but the macro now fails in cases like: > if (suspiciousstuff) > CHECK_ERR; > else > /* everything peachy keen. */ > > (It's VERY similar to the problem you get using assembly macros that can > expand to more than one instruction after SKIP instructions.) > > A common way to fix this in C is to make your macro a block statement > that > executes exactly once: > #define CHECK_ERR do { \ > errcode = get_error(); \ > if (errcode) { \ > print_error(errcode) \ > } while (0) > Now, not matter how complex the code inside the do ... while > statement, it > it lexes/parses the same as a single C statement. > > BillW > I must be dense. Why not just use a compound statement e.g. #define CHECK_ERR { \ errcode = get_error(); \ if (errcode) { \ print_error(errcode) \ } \ } Regards Sergio -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist