C Swapping Two Numbers Program


Swapping two numbers using pointers in C is a common exercise that helps illustrate the use of pointers for manipulating variables. In this program, we will define a function that takes two pointers as parameters and swaps the values they point to.

Program: Swap Two Numbers Using Pointers

Here’s a simple C program that demonstrates how to swap two numbers using pointers:

#include <stdio.h> // Function to swap two numbers using pointers void swap(int *a, int *b) { int temp; // Temporary variable to hold one of the values temp = *a; // Store the value pointed to by a in temp *a = *b; // Assign the value pointed to by b to the location pointed to by a *b = temp; // Assign the value in temp to the location pointed to by b } int main() { int num1, num2; // Declare two integers // Input numbers from the user printf("Enter two numbers to swap:\n"); printf("Number 1: "); scanf("%d", &num1); printf("Number 2: "); scanf("%d", &num2); printf("\nBefore swapping: \n"); printf("Number 1: %d\n", num1); printf("Number 2: %d\n", num2); // Call the swap function swap(&num1, &num2); // Pass the addresses of num1 and num2 printf("\nAfter swapping: \n"); printf("Number 1: %d\n", num1); printf("Number 2: %d\n", num2); return 0; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: This is included for input and output functions like printf and scanf.
  2. Function Declaration:

    • void swap(int *a, int *b): This function takes two integer pointers as parameters. The void return type indicates that this function does not return a value.
  3. Swapping Logic:

    • Inside the swap function:
      • A temporary integer variable temp is declared.
      • The value pointed to by a is stored in temp.
      • The value pointed to by b is assigned to the location pointed to by a.
      • Finally, the value in temp (originally from a) is assigned to the location pointed to by b. This effectively swaps the values of the two integers.
  4. Main Function:

    • Two integer variables num1 and num2 are declared to hold the user input.
    • The program prompts the user to enter two numbers and stores them in num1 and num2 using scanf. The & operator is used to get the addresses of these variables.
    • Before calling the swap function, the program prints the values of num1 and num2.
    • The swap function is called with the addresses of num1 and num2 as arguments.
    • After the swap operation, the new values of num1 and num2 are printed.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc swap_using_pointers.c -o swap_using_pointers
  2. Execute the Program:

    ./swap_using_pointers

Example Output

When you run the program, the output will be similar to:

Enter two numbers to swap: Number 1: 5 Number 2: 10 Before swapping: Number 1: 5 Number 2: 10 After swapping: Number 1: 10 Number 2: 5

Conclusion

The program demonstrates how to swap two numbers using pointers in C. By passing the addresses of the variables to the swap function, we can modify the original variables directly. This exercise highlights the power of pointers for manipulating data in C, enabling more efficient and flexible code.