Temp Variable
Classic
Save, overwrite, restore.
Swapping exchanges two variables so each holds the other’s old value: a=5, b=10 becomes a=10, b=5. You will use a temp variable in main, a pointer-based helper function, and an optional XOR trick for integers — plus a live preview and worked examples.
Classic
Save, overwrite, restore.
int *a, *b
Swap the caller's variables.
Integers
Interview curiosity, not style.
Order
Save before you overwrite.
Try 5 & 10
Swap custom integers instantly.
All variants
Constant time and space.
Swapping means exchanging the values of two variables without losing either one. If you write a = b first, the old a is gone forever — that is why beginners learn a temporary holder.
In C, a function must take int * pointers (or you swap plain variables inside main). Interviews love the classic three-step swap because it shows up in sorting and every systems language.
Swap is a building block for sorting, partitioning, and many in-place algorithms.
temp holds a copy.
Pass &num1, &num2.
Integers only.
7, 7 stays 7, 7.
In short: exchange values without losing either — prefer temp (and pointers inside helpers) in C.
Given two variables, exchange their values so each holds what the other used to hold.
/* Before: a = 5, b = 10 */
/* After: a = 10, b = 5 */ | Item | Type | Description |
|---|---|---|
a, b | int | Values to exchange (XOR needs integer types). |
| Result | same types | a holds old b; b holds old a. |
temp | same as a | Optional holding spot. |
temp = a
a = b
b = temp | Method | Idea | Notes |
|---|---|---|
| Temp variable | Save, overwrite, restore | Universal across languages |
| Pointers | swap(int *a, int *b) | Needed outside main |
| XOR | a ^= b; b ^= a; a ^= b | Integers; interview trick |
| Goal | Pattern |
|---|---|
| Temp save | temp = a |
| Overwrite | a = b |
| Restore | b = temp |
| Pointers | swap(&x, &y) |
| XOR (ints) | a ^= b; b ^= a; a ^= b |
| Equal values | Swap still correct; no visible change |
Same result — different packaging.
temp = aClearest cross-language idea
*a / *bNeeded for a swap function
a ^= bIntegers only; less readable
a = b firstLoses the old a
Reach for a swap whenever two values need to trade places in place.
First assignment / memory drill.
Bubble / selection / partition swaps.
temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
Temp swap proves shared concepts.
Stick to temp then.
Key benefit: one tiny idea — save before overwrite — that shows up in almost every in-place algorithm.
Enter two integers and swap them with a temporary variable (same idea as the C examples).
Three complete C programs — temp in main, a pointer helper, and XOR for integers. Click View Output to reveal sample console results.
The classic three-step swap that works in almost every language.
Save the first value, overwrite it, then restore into the second variable — all inside main.
#include <stdio.h>
int main(void) {
int num1 = 5;
int num2 = 10;
int temp;
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
temp = num1;
num1 = num2;
num2 = temp;
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
} temp keeps 5 while num1 becomes 10. Then num2 receives the saved 5. No pointers needed when everything lives in one function.
A helper needs pointers so the caller’s variables actually change.
Classic interview style: pass addresses so the swap survives after the function returns.
#include <stdio.h>
void swapNumbers(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main(void) {
int num1 = 5;
int num2 = 10;
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swapNumbers(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
} &num1 passes the address of num1. Inside the function, *a reads or writes the original variable. A plain void swap(int x, int y) would only swap copies.
Works for integers. Prefer temp in real C code; keep XOR as an interview curiosity. Guard when both pointers are the same address.
#include <stdio.h>
void swapXor(int *a, int *b) {
if (a == b) {
return;
}
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
int main(void) {
int num1 = 5;
int num2 = 10;
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swapXor(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
} Three XOR steps rearrange the bit patterns so a and b trade places. The a == b address guard avoids wiping a value to 0 when both parameters point at the same variable.
Save the first value.
Copy the second into the first.
Restore the saved value into b.
Neither original was lost.
Follow the temp variable through each assignment.
| Step | a | b | temp |
|---|---|---|---|
| start | 5 | 10 | — |
| temp = a | 5 | 10 | 5 |
| a = b | 10 | 10 | 5 |
| b = temp | 10 | 5 | 5 |
If you assign a = b first without saving, the original 5 disappears.
Where swaps show up beyond the interview prompt.
Assignment and memory basics.
Example: temp swap of 5, 10.
Bubble and selection swaps.
Example: adjacent pairs.
temp between arr[i] and arr[j].
Example: in-place reorder.
Quicksort-style exchanges.
Example: pivot swaps.
Why overwrite order matters.
Example: walkthrough table.
Continue the interview chain.
Example: related CTA.
Pro Tip: say “save before overwrite” before you write the three lines.
Why learning all three approaches is useful.
Same idea in C, Java, JS, and more.
Helpers change the caller’s variables.
O(1) time and space for all variants.
Useful trivia once you know the readable form.
Pro Tip: lead with temp (and pointers in helpers); mention XOR only if asked about “without a temp”.
Small habits that keep swap solutions interview-ready.
Never a = b as the first step alone.
Use pointers when the swap lives in a helper.
Makes demos and debugging clearer.
Integers only; mention readability cost.
7, 7 should remain 7, 7.
Pro Tip: sanity-check (5, 10), (7, 7), and (-4, 20) — if those three work, you are solid.
Mistakes that commonly break swap programs.
Writing a = b without saving a.
→ Use temp or pointer swap.
Floats and strings do not XOR.
→ Use temp instead.
Missing one of the three ^= steps.
→ Prefer temp in C.
Expecting failure when a == b.
→ Swap is still correct.
Clever but harder to maintain.
→ Use the readable form.
Handle these before claiming the swap is complete.
Swap is still correct.
Temp (or pointer + temp) is preferred in C.
-4 and 20 swap fine.
0, 3 becomes 3, 0.
Skip XOR.
arr[i], arr[j] = arr[j], arr[i]
Handy follow-ups interviewers sometimes ask.
*. That is why a helper takes int * addresses.Try these variations to lock in the pattern.
int * and use temp.Quick Takeaway: save before overwrite — use pointers when swapping inside a function.
| Version | Time | Extra space |
|---|---|---|
| Temp swap | O(1) | O(1) |
| Pointer swap | O(1) | O(1) |
| XOR swap | O(1) | O(1) |
Swapping two variables does a fixed number of assignments regardless of the values.
Swapping exchanges two values without losing either one. Learn the temp-variable pattern, use pointers for a reusable swap function, and treat XOR as optional trivia.
Practice the three examples above, then continue to triangular numbers.
Save first — pass addresses into helpers.
Exchange values safely — then use the idea in sorting.
save / set / restore
Classic*a / *b + temp
Cints only
Trickstill correct
EdgeO(1)
AnalysisSwapping values is one of the smallest building blocks in sorting. In C, the three-line pattern with a temp variable (and pointers when swapping inside a function) is the clearest everyday approach.
Learn how to check whether a number is triangular in C.
8 people found this page helpful