Ok, so I've always wanted to be able to return a value from a complex multi-line macro but have never figured out how to do it.
Ternary is not an option for the kinds of things I'm talking about.
Today I saw some odd looking macro code that was able to perform this magic.
Here is very reduced example of the "magic"
as it can be applied in the AVR world.
#includeuint8_t var8; #define avr_rd(type, reg) ({uint8_t f = type ## reg; f;}) void mtest(void) { var8 = avr_rd(PIN, D); }
The "magic" is the "var;" line.
The original code I saw used "var >>=0;" but
that is essentially the same as this.
My question is why does this work?
Normally the optimizer will remove references to unused rvalues unless they referencing volatile data.
So something like 6; or var; would be totally removed.
So why does this work inside a macro to return the value?
Is this behavior something that is guaranteed to work across all C implementations?
Have I missed out on this known behavior all these years?
--- bill