C语言中交换两个数值的方法

 //方法1
    int  one = 1;
    int two = 2;
    int temp = 0;
    temp = one;
    one = two;
    two = temp;
    printf("one = %d ", one);
    printf("two = %d ", two);
    
        //方法2
    int a1 = 5;
    int a2 = 8;
    a1 = a2 - a1;
    a2 = a2 - a1;
    a1 = a1 + a2;
    printf("a1 = %d ", a1);
    printf("a2 = %d ", a2);
        //方法3
    
    int c = 10, d = 20;
    c = c ^ d;
    d = d ^ c;
    c = c ^ d;
    printf("c = %d ", c);
    printf("d = %d ", d);
 
        //方法4
    int e = 10, f = 20;
    int *g = &e, *h = &f;
    int tep = *g;
    *g = *h;
    *h = tep;
    printf("e = %d ", e);
    printf("f = %d ", f);

 
原文地址:https://www.cnblogs.com/tian-sun/p/4307608.html