01
Write a C program to swap two numbers without using a third variable.
Show answer
Using arithmetic or XOR through pointers:
void swap(int *a, int *b) {
if (a == b) return;
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
The a == b check matters because XOR swapping a variable with itself sets it to zero. In real code a temporary variable is clearer and just as fast.