Simple program to swap two numbers without third (aka) temporary variable:
int main(){
int a, b;
printf("\n\rEnter 2 numbers:\n\r");
scanf("%d%d",&a,&b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("\n\rAfter swap - %d and %d\n\r", a , b);
}
We know xor(^) is used to toggle the bit. By using xor we can swap two numbers without the third or temporary variable.
Let us take a example, a = 10 and b = 5
a = 10 (1010) ^ 5 (0101) = 15 (1101)
b = 15 (1101) ^ 5 (0101) = 10 (1010)
a = 15 (1101) ^ 10(1010) = 5(1010)