Originally Posted by
coder222
I think that's matter of style. Also, I would prefer variables begin with underscore so that they won't clash with function or variable definitions in header files.
1. The do...while(0) macro trick is not a matter of style, it's to allow the macro to be used in places where a statement normally would be.
2. The underscore trick is only needed for variables you create inside your function-like macro:
Code:
#include <stdio.h>
#define swap(type, x, y) do {\
type _t = x; /* Naming this t will cause problems */\
/* in the below code. */\
x = y;\
y = _t;\
} while(0)
int main(void)
{
int x = 100;
int t = 200;
int y = 300;
printf("Swap x/t or t/y? [1/2]: ");
char resp = getchar();
if (resp == '1') swap(int,x,t); /* This will fail unless swap is
wrapped with do {...} while(0) */
else swap(int,t,y);
printf("%d %d %d\n", x, t, y);
return 0;
}