Uploaded November 2026 | Updated November 2026, 2 days ago
Learn how to write a C program to Swap Two Numbers using Pointers and Functions. This beginner-friendly tutorial demonstrates how to pass memory addresses as function arguments and modify the original variables using Pointer Dereferencing.
In this tutorial, we first create two integer variables, num1 and num2, and initialize them with the values 10 and 20. We use printf() to display their values before swapping.
Next, we create a swap() function that accepts two Pointer to int parameters (int *). Instead of passing the values of the variables, we pass their memory addresses using the Address-of Operator (&).
Inside the swap() function, we use Pointer Dereferencing (*) to access and modify the original variables. We also introduce a temporary variable to help exchange the two values without losing either of them.
We implement the swapping logic in three steps:
1. Store the value pointed to by the first pointer in a temporary variable.
2. Copy the value pointed to by the second pointer into the object pointed to by the first pointer.
3. Copy the temporary value into the object pointed to by the second pointer.
After implementing the swap function, we discuss Function Prototypes in C. Since the swap() function is defined after main(), we declare its prototype before main() so that the compiler knows its return type and parameter types.
Finally, we execute the program and verify that the values have been swapped successfully. Before swapping, the variables contain 10 and 20. After swapping, they contain 20 and 10.
This example helps you understand Passing Pointers to Functions, Pointer Parameters, Memory Addresses, Pointer Dereferencing, Function Prototypes, and modifying variables through functions in C programming.
Our Website
LearningLad.com
Social Media
Facebook facebook.com/LearningLad
Twitter twitter.com/LearningLadEdu
Instagram instagram.com/LearningLadOfficial
Learn how to write a C program to Swap Two Numbers using Pointers and Functions. This beginner-friendly tutorial demonstrates how to pass memory addresses as function arguments and modify the original variables using Pointer Dereferencing.
In this tutorial, we first create two integer variables, num1 and num2, and initialize them with the values 10 and 20. We use printf() to display their values before swapping.
Next, we create a swap() function that accepts two Pointer to int parameters (int *). Instead of passing the values of the variables, we pass their memory addresses using the Address-of Operator (&).
Inside the swap() function, we use Pointer Dereferencing (*) to access and modify the original variables. We also introduce a temporary variable to help exchange the two values without losing either of them.
We implement the swapping logic in three steps:
1. Store the value pointed to by the first pointer in a temporary variable.
2. Copy the value pointed to by the second pointer into the object pointed to by the first pointer.
3. Copy the temporary value into the object pointed to by the second pointer.
After implementing the swap function, we discuss Function Prototypes in C. Since the swap() function is defined after main(), we declare its prototype before main() so that the compiler knows its return type and parameter types.
Finally, we execute the program and verify that the values have been swapped successfully. Before swapping, the variables contain 10 and 20. After swapping, they contain 20 and 10.
This example helps you understand Passing Pointers to Functions, Pointer Parameters, Memory Addresses, Pointer Dereferencing, Function Prototypes, and modifying variables through functions in C programming.
Our Website
LearningLad.com
Social Media
Facebook facebook.com/LearningLad
Twitter twitter.com/LearningLadEdu
Instagram instagram.com/LearningLadOfficial










