C programming provides several ways to swap two variables, meaning we exchange their values with each other. Which helps you better to work with the variables in a programming language.
This article will show you three methods to swap variables: using a temporary variable, without using a temporary variable, and using pointers. Along with each method, we’ll also walk through a step-by-step dry run of the code to make it easy to understand.
Method 1: Using a Temporary Variable
This is the simplest way to swap two variables. We use a third variable, often called “temp,” to hold one value while swapping the two.
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
int temp;
printf("Before swapping: a = %d and b = %d\n", a, b);
temp = a; // Step 1: Store the value of a in temp
a = b; // Step 2: Assign the value of b to a
b = temp; // Step 3: Assign the value of temp to b
printf("After swapping: a = %d and b = %d\n", a, b);
return 0;
}
Dry Run:
- Initial values: a = 5, b = 10, temp is not set yet.
- Step 1: temp = a (temp gets the value of a → temp = 5). Now: a = 5, b = 10, temp = 5.
- Step 2: a = b (a gets the value of b → a = 10). Now: a = 10, b = 10, temp = 5.
- Step 3: b = temp (b gets the value of temp → b = 5). Now: a = 10, b = 5, temp = 5.
Result: a = 10, b = 5 – the values have been swapped.
Method 2: Without Using a Temporary Variable
We can swap two variables without using a third variable. This method uses basic arithmetic, but be careful when working with large numbers, as it might cause errors like overflow.
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
printf("Before swapping: a = %d and b = %d\n", a, b);
a = a + b; // Step 1: Add a and b and store the result in a
b = a - b; // Step 2: Subtract b from the new value of a
a = a - b; // Step 3: Subtract the new value of b from a
printf("After swapping: a = %d and b = %d\n", a, b);
return 0;
}
Dry Run:
- Initial values: a = 5, b = 10.
- Step 1: a = a + b → a = 5 + 10 → a = 15. Now: a = 15, b = 10.
- Step 2: b = a – b → b = 15 – 10 → b = 5. Now: a = 15, b = 5.
- Step 3: a = a – b → a = 15 – 5 → a = 10. Now: a = 10, b = 5.
Result: a = 10, b = 5 – the values have been swapped.
Method 3: Using Pointers
In this method, we use pointers to directly change the values in memory. This is useful when swapping larger data types like arrays or when you want to reuse the swap function multiple times in different parts of the program.
#include <stdio.h>
void swap(int* a, int* b) {
int temp;
temp = *a; // Step 1: Store the value at memory location a in temp
*a = *b; // Step 2: Assign the value at memory location b to memory location a
*b = temp; // Step 3: Assign the value in temp to memory location b
}
int main() {
int a = 5;
int b = 10;
printf("Before swapping: a = %d and b = %d\n", a, b);
swap(&a, &b); // Call swap function, passing the addresses of a and b
printf("After swapping: a = %d and b = %d\n", a, b);
return 0;
}
Dry Run:
- Initial values: a = 5, b = 10. The function receives the addresses of a and b (let’s call them *a and *b).
- Step 1: temp = *a → temp = 5. Now: *a = 5, *b = 10, temp = 5.
- Step 2: *a = *b → *a = 10. Now: *a = 10, *b = 10, temp = 5.
- Step 3: *b = temp → *b = 5. Now: *a = 10, *b = 5, temp = 5.
Result: a = 10, b = 5 – the values have been swapped.
Wrap Up
In conclusion, swapping variables in C can be done in different ways, each with its pros and cons. The method with a temporary variable is the easiest to understand. The method without a temporary variable saves memory but should be used carefully with larger numbers. The pointer method is powerful when working with functions or large data types.
Understanding these methods will help you in manipulating data effectively in your programs. Make sure to practice and test your code to ensure it works as expected.