3 ms·
Same with what I suggested: #include <stdio.h> #define MYMACRO(a,b,c) \ ({ \ typeof (c) _c = (c); \ a = b + _c; \ b = _c * 2; \
by paulbaumgart 17y ago
Same with what I suggested:
#include <stdio.h>
#define MYMACRO(a,b,c) \
({ \
typeof (c) _c = (c); \
a = b + _c; \
b = _c * 2; \
})
int main() {
int a = 0, b = 0;
if (1)
MYMACRO(a,b,1);
else
MYMACRO(a,b,2);
printf("%d, %d\n", a, b);
}
-----
$ gcc -o test test.c
$ ./test
1, 2
Edit: Just for context: this was responding to a post pointing out that the do-while construct allows you to use a semi-colon after the macro invocation.
- doug11235 17y agoAnd another feature I like about ({ }) macros (anyone know the proper name here?) is you can return values. Clearly more useful in an example a bit more complex but... #define ADD_ONE(a) ({ a + 1; }) x = ADD_ONE(2);
- tedunangst 17y agoThey are called statement expressions.