请参阅下面的示例,其中我将交换两个变量的值。 do-while(0)结构很不错
#include <stdio.h>
#define swap(x,y,T) do {
T temp = (*x);
(*x) = (*y);
(*y) = temp;
} while (0)
int main(void)
{
int a = 5;
int b = 9;
printf("Value of a and b before swaping
");
printf("a = %d
",a);
printf("b = %d
",b);
//Swap the number
swap(&a,&b,int);
printf("
Value of a and b After swaping
");
printf("a = %d
",a);
printf("b = %d
",b);
return 0;
}