如何在C中定义多行宏定义?

请参阅下面的示例,其中我将交换两个变量的值。 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\n");
  printf("a = %d\n",a);
  printf("b = %d\n",b);
  
  //Swap the number
  swap(&a,&b,int);
  
  
  printf("\n\nValue of a and b After swaping\n");
  printf("a = %d\n",a);
  printf("b = %d\n",b);
  
    return 0; 
}

c中çcå®

posted @ 2019-09-04 08:39  wdliming  阅读(456)  评论(0编辑  收藏  举报