交换两个变量的不同实现方式
#include <stdio.h> #define SWAP1(a,b) \ { \ int temp = a; \ a = b; \ b = temp; \ } #define SWAP2(a,b) \ { \ a = a + b; \ b = a - b; \ a = a - b; \ } #define SWAP3(a,b) \ { \ a = a ^ b; \ b = a ^ b; \ a = a ^ b; \ } int main() { int a = 1; int b = 2; SWAP1(a,b); //需要一个临时变量 SWAP2(a,b); //效率低,而且存在溢出的可能 SWAP3(a,b); return 0; }