#include<stdio.h>
int main()
{
int a=20,b=30;
a=a+b;
b=a-b;
a=a-b;
return 0;
}
Can I use this program to swap both positive and negative values.
#include<stdio.h>
int main()
{
int a=20,b=30;
a=a+b;
b=a-b;
a=a-b;
return 0;
}
Can I use this program to swap both positive and negative values.
The algorithm for swapping variables without a temporary holder uses the bitwise XOR operation, not subtraction. So, using variables named a
and b
, we'd have:
a=a^b;
b=a^b;
a=a^b;
EDIT: Your code does work for the few cases I tested it with using some integral values for a
and b
, but XOR is the conventional way of doing it. As commenter @wildplasser pointed out, the reason XOR is the convention is that integer addition and subtraction can overflow, giving incorrect results, whereas XOR cannot.
This method will not work if a+b
overflows as this invokes undefined behavior.
It's better to use the XOR method. Even better, use a temp anyway as it's more clear.
As others have pointed out, you should use XOR instead of arithmetic operators to avoid UB due to signed overflow.
But keep in mind that in most common processors (x86, ARM, 68k, etc.) 3 logical operations will be slower (and consume more energy) than 3 moves between registers. So, provided enough registers are available (which in your example is most surely the case) and optimization enabled, using a temporary will be faster than bit twiddling. Finally, some architectures have an exchange instruction (e.g. xchg
on x86), a smart enough compiler should be able to use it, and that would be even faster.
The moral of the story - beware premature optimization.
There is no preferred way to swap two variables without using a temporary variable in C.
The preferred way to swap two variables in C is to use a temporary variable.
Swapping without using a temporary is an obsolete problem. It might once have been an interesting problem back when we were writing (a) assembly language code for (b) machines with very few registers and not much memory. But when we're writing in a higher level language like C, on a machine with ample memory and registers, there is nothing -- nothing! -- positive to be gained by trying to eliminate the temporary variable.
Asking "What's the best way to swap two variables without using a temporary?" is a lot like asking "What's the best way to get my horse to pull my buggy without using a buggy whip?".
(Sorry for the opinionated answer, but while swapping without using temporaries might be an interesting intellectual problem, it is a an error-prone waste of time in any practical programming situation. The likelihood that you will improve the performance of your program by eliminating the temporary is zero. The likelihood that you will introduce subtle bugs is substantially nonzero.)
Yep it can be used for both positive and negative values but the problem with this method is when the numbers are large a+b can go out of the range and that can lead to a strange behaviour. Using temp variable for swapping is a good and simple way so use that whenever possible.