вторник, 4 марта 2014 г.

How to return value from C macro

Usually I use variable name in C macro as out parameter (it's standard way in Tcl, for example). But there is another way, something like in functional languages, with expression. See:
#define GETX(X) ({ \
        int x = (X)*2; \
        x; \
        })

void main()
{
    printf("%d\n", GETX(56));
}
This example shows hot to return value from GETX(), which really is expression. Works in GCC :)