Introduction
Swapping two numbers means exchanging their values. Pointers in C allow you to directly access and manipulate the memory addresses of variables, making it possible to swap the values of two variables using a function. This guide will show you how to write a C program to swap two numbers using pointers.
Example:
- Input: Two variables, say
a = 5andb = 10. - Output: After swapping,
a = 10andb = 5.
Problem Statement
Create a C program that:
- Declares two integer variables and initializes them.
- Uses a function that takes pointers as parameters to swap the values of the two variables.
- Displays the values of the variables before and after swapping.
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Write a Function to Swap Two Numbers: Define a function that takes two pointers as arguments and swaps the values they point to.
- Declare and Initialize Variables: Declare two integer variables and initialize them with values.
- Call the Swap Function: Pass the addresses of the two variables to the swap function.
- Display the Values Before and After Swapping: Use
printfto display the values before and after calling the swap function.
C Program to Swap Two Numbers Using Pointers
#include <stdio.h>
// Step 2: Define the function to swap two numbers using pointers
void swap(int *x, int *y) {
int temp;
temp = *x; // Store the value pointed by x in temp
*x = *y; // Copy the value pointed by y into x
*y = temp; // Copy the value stored in temp into y
}
int main() {
// Step 3: Declare and initialize variables
int a = 5, b = 10;
// Display the values before swapping
printf("Before swapping: a = %d, b = %d\n", a, b);
// Step 4: Call the swap function
swap(&a, &b);
// Display the values after swapping
printf("After swapping: a = %d, b = %d\n", a, b);
return 0; // Return 0 to indicate successful execution
}
Explanation
Step 2: Define the Function to Swap Two Numbers
- The
swapfunction takes two pointersxandyas arguments. These pointers point to the variables that need to be swapped. - Inside the function:
- The value pointed to by
xis stored in a temporary variabletemp. - The value pointed to by
yis then copied into the location pointed to byx. - Finally, the value stored in
tempis copied into the location pointed to byy.
- The value pointed to by
- This effectively swaps the values of the two variables.
Step 3: Declare and Initialize Variables
- The variables
aandbare declared and initialized with the values5and10, respectively.
Step 4: Call the Swap Function
- The
swapfunction is called with the addresses ofaandbas arguments. The&operator is used to pass the addresses.
Display Values Before and After Swapping
- Before calling the
swapfunction, the original values ofaandbare printed. - After calling the
swapfunction, the values ofaandbare printed again to confirm that they have been swapped.
Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example Output:
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
Conclusion
This C program demonstrates how to use pointers to swap the values of two variables. It covers basic concepts such as pointer declaration, using the address-of operator, and manipulating values through pointers, making it a useful example for beginners learning C programming.