1 #include <stdio.h> 2 3 #define swap_macro_0(a, b) 4 do { 5 typeof(a) c; 6 c = a; 7 a = b; 8 b = c; 9 } while(0) 10 11 #define swap_macro_1(a, b) 12 do { 13 a = a + b; 14 b = a - b; 15 a = a - b; 16 }while(0) 17 18 #define swap_macro_2(a, b) 19 do { 20 a = a ^ b; 21 b = a ^ b; 22 a = a ^ b; 23 }while(0) 24 25 #define swap_macro_3(a, b) 26 do { 27 a = a * b; 28 b = a / b; 29 a = a / b; 30 }while(0); 31 32 #define swap_macro_4(a, b) 33 do { 34 a = a + b - (b = a); 35 }while(0); 36 37 int main() 38 { 39 int type, a, b; 40 41 scanf("%d, %d, %d", &type, &a, &b); 42 printf("before a = %d, b = %d ", a, b); 43 switch(type) 44 { 45 case 0: 46 { 47 swap_macro_0(a, b); 48 break; 49 } 50 case 1: 51 { 52 swap_macro_1(a, b); 53 break; 54 } 55 case 2: 56 { 57 swap_macro_2(a, b); 58 break; 59 } 60 case 3: 61 { 62 swap_macro_3(a, b); 63 break; 64 } 65 case 4: 66 { 67 swap_macro_4(a, b); 68 break; 69 } 70 default: 71 { 72 printf("unknown swap type! "); 73 return 0; 74 } 75 } 76 printf("after a = %d, b = %d ", a, b); 77 return 0; 78 }