Swapping numbers is something you often do in programming. Using pointers in C makes it super efficient. Imagine you’re swapping values in a function — that’s where pointers come into play. Here's how you can swap two numbers with them.
Prerequisites for the C Program to Swap Two Numbers Using Pointers
Before diving into the code, ensure you're familiar with these concepts:
- Understanding of int variables
- Pointers and memory addresses
<stdio.h>for input/output operations- The concept of functions in C
- Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Swap Two Numbers Using Pointers
Here's the complete source code for swapping two numbers with pointers:
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a, b;
"Enter the first number: ";
scanf("%d", &a);
"Enter the second number: ";
scanf("%d", &b);
swap(&a, &b);
printf("After swapping, first number = %d\n", a);
printf("After swapping, second number = %d\n", b);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Swap Two Numbers Program
When you run this program, you'll see the input and the swapped output like this:
Enter the first number: 5
Enter the second number: 10
After swapping, first number = 10
After swapping, second number = 5
Algorithm to Swap Two Numbers Using Pointers in C
Swapping two numbers with pointers involves accessing direct memory locations. Here's the logic step by step.
- Read two integers
aandbfrom the user. - Define a
swapfunction that takes two pointers as parameters. - In the
swapfunction, store the value of the first pointer in a temporary variable. - Assign the value pointed to by the second pointer to the first pointer.
- Place the temporary variable's value in the location pointed by the second pointer.
- Print the swapped values.
flowchart TD A([Start]) --> B[/Input a and b/] B --> C[Define swap function] C --> D[Call swap(&a, &b)] D --> E[Inside swap: temp = *x] E --> F[*x = *y] F --> G[*y = temp] G --> H[End of swap function] H --> I([Print swapped values]) I --> J([End])
How the C Program to Swap Two Numbers Using Pointers Works
Let's walk through the code line by line to understand its operation.
- The program starts by including the standard input-output library,
<stdio.h>, to handle I/O operations. - The
swapfunction is defined to take twointpointers as arguments. It performs the swapping by using a temporary variable to hold one value while swapping. - In the
mainfunction, two integer variables,aandb, are declared to store the numbers entered by the user. - The
printfandscanffunctions are used for input/output operations to read the numbers. - The
swapfunction is called using the addresses ofaandb, allowing the function to modify the original variables. - Finally, the program outputs the swapped numbers using
printfstatements to display the results. - The program ends with
return 0, indicating successful execution.
Quick Quiz — test your understanding
Tap an option to check your answerWhat does the swap function use to exchange the values of two variables?
Time and Space Complexity of the C Program to Swap Two Numbers Using Pointers
Analyzing the computational complexity for this program.
Time Complexity: O(1). The swap operation involves a fixed number of steps: storing one value in a temporary variable, then performing two assignments. It doesn't depend on the size of input, so it remains constant.
Space Complexity: O(1). Only three variables are utilized in total: a, b, and temp in the swap function. The space requirement is constant regardless of input size.
Common mistake: Forgetting to pass the addresses of variables to the swap function will result in no swapping.
Key takeaway:Pointers give you a strong way to manipulate data directly by accessing memory locations.
FAQs About Swapping Two Numbers in C Using Pointers
Here are some frequently asked questions about this program.
scanf is great for user input.& operator grabs the memory address of a variable, giving the function access to the original data.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Store Information of Students Using Structure
- C Program to Add Two Distances in Inch-Feet Using Structures
- C Program to Store Data in Structures Dynamically
- C Program to Add Two Complex Numbers by Passing Structure
- C Program to Find Largest Number Using Dynamic Memory Allocation
Conclusion
Swapping numbers with pointers gives you a peek into memory manipulation. It's a cool trick. You might wanna check out pointer arithmetic next.
Try swapping with similar tasks in the C compiler, or test what you know with our C language quiz.