Is there a built in swap function in C?

前端 未结 11 2158
北恋
北恋 2020-12-15 16:01

Is there any built in swap function in C which works without using a third variable?

11条回答
  •  再見小時候
    2020-12-15 16:15

    #define swap(T, x, y) \
        {                 \
            T tmp = x;    \
            x = y;        \
            y = tmp;      \
        }
    
    int main()
    {
        int a = 10;
        int b = 20;
        printf("a=%d b=%d\n", a, b);
        swap(int, a, b);
        printf("a=%d b=%d\n", a, b);
    
        return 0;
    }
    

提交回复
热议问题