News for hackers. Stuff that matters.
Re:Tips on writing C macros by paulbaumgart on Tuesday August 04, 2009 @08:44PM (#742543)
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.
Re:Tips on writing C macros by doug11235 on Tuesday August 04, 2009 @08:49PM (#742553)
And 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);

Re:Tips on writing C macros by tedunangst on Tuesday August 04, 2009 @09:15PM (#742599)
They are called statement expressions.