Swap Two Numbers Using Pointers in C | Swap Function & Pointer Dereferencing @LearningLad
Swap Two Numbers Using Pointers in C | Swap Function & Pointer Dereferencing  @LearningLad
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
Swap Two Numbers Using Pointers in C | Swap Function & Pointer DereferencingHow to Compile and Run a C++ Program from Command Prompt in Windows 10Simple Trick to Open up Command Prompt ( cmd ) in a Folder with current location as Path Windows 10Displaying float double long double values in Scientific Notation in C Programming Language2D Arrays with Loops in C | User Input & Variable Length 2D ArraysHow to Set up Visual Studio Code for Python ProgrammingHow to Add Two Numbers in Python | Simple User Input Program TutorialArray of Strings in C | 2D Character Arrays, Input & Accessing StringsC Calculator Program Using Do While Loop & Switch Statement | Beginner TutorialSize Modifiers in C Programming | Short, Long & Unsigned Integers ExplainedHow to Install Python on Windows 11 (Latest Version) | Step-by-Step Guide for BeginnersWhat is a Structures | How to define Structures | C Language Tutorial
LearningLad |

Swap Two Numbers Using Pointers in C | Swap Function & Pointer Dereferencing

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER