Possible Duplicate:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
In my c code i need to use macro. I compiled my code using gcc. Followings are my experiment:
#define temp(a, b) a++; \
b++;
foo()
{
temp(x, y);
}
^^^^^ this will work fine
#define temp(a, b) a++; \
b++;
foo()
{
if(z)
temp(x, y);
else
foo();
}
^^^^^ this will not work
#define temp(a, b) { \ <<<
a++; \
b++; \
} <<<
foo()
{
if(z)
temp(x, y);
else
foo();
}
^^^^^ this will also not work
#define temp(a, b) do { \ <<<
a++; \
b++; \
} while(0) <<<
foo()
{
if(z)
temp(x, y);
else
foo();
}
^^^^^ this will work :)