Often, in macros, you will see people use a do { ... } while(0)
to swallow the semicolon. I just came across an example where they use ({ ... })
instead, and it seems to not only swallow the semicolon, but seems to allow you to return a value as well:
#define NEW_MACRO() ({ int x = 1; int y = 2; x+y; })
if(1)
val = NEW_MACRO();
else
printf("this never prints");`
val
would come out being 3. I can't find any documentation on it, so I'm a bit wary of it. Are there any gotcha's with this method?