5 ms·
Isn't "do { ... } while (0)" the same as just "{ ... }"? I sometimes use "{ ... }" to limit the scope of a variable that is only needed for a small section of
by AlexandrB 15y ago
Isn't "do { ... } while (0)" the same as just "{ ... }"?
I sometimes use "{ ... }" to limit the scope of a variable that is only needed for a small section of code.
- enqk 15y agodo { ... } while (0) makes your macro behave like a statement (i.e. the semicolon is mandatory)
- rcfox 15y ago({ ... }) does this too. (Is this GCC-specific?)
- mansr 15y agoThat is a gcc extension. The construct as a whole has the value of the last statement executed within. Usually an inline function is preferable since it achieves the same thing using only standard syntax.
- silentbicycle 15y agoYes. I don't include the trailing semicolon in macro definitions, because I expect the macro to be followed by them: MACRO(foo);
- huhtenberg 15y agoFormer lets break'ing out, latter doesn't.