C Program to Swap Two Numbers Using Pointers

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 = 5 and b = 10.
  • Output: After swapping, a = 10 and b = 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

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Write a Function to Swap Two Numbers: Define a function that takes two pointers as arguments and swaps the values they point to.
  3. Declare and Initialize Variables: Declare two integer variables and initialize them with values.
  4. Call the Swap Function: Pass the addresses of the two variables to the swap function.
  5. Display the Values Before and After Swapping: Use printf to 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 swap function takes two pointers x and y as arguments. These pointers point to the variables that need to be swapped.
  • Inside the function:
    • The value pointed to by x is stored in a temporary variable temp.
    • The value pointed to by y is then copied into the location pointed to by x.
    • Finally, the value stored in temp is copied into the location pointed to by y.
  • This effectively swaps the values of the two variables.

Step 3: Declare and Initialize Variables

  • The variables a and b are declared and initialized with the values 5 and 10, respectively.

Step 4: Call the Swap Function

  • The swap function is called with the addresses of a and b as arguments. The & operator is used to pass the addresses.

Display Values Before and After Swapping

  • Before calling the swap function, the original values of a and b are printed.
  • After calling the swap function, the values of a and b are 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top