Search code examples
cswap

Is there a built in swap function in C?


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


Solution

  • No.
    C++ builtin swap function: swap(first,second);
    Check this: http://www.cplusplus.com/reference/algorithm/swap/

    You can use this to swap two variable value without using third variable:

    a=a^b;
    b=a^b;
    a=b^a;
    

    You can also check this:

    https://stackoverflow.com/questions/756750/swap-the-values-of-two-variables-without-using-third-variable

    How to swap without a third variable?