Swap Two Numbers using Pointers in C Language

Swap-Two-Numbers-using-Pointers-in-C-Language

Program Description:

Write a Program to Swap Two Numbers using Pointers in C programming language. The program will accept two integer numbers from the user and swaps the values of the given number using the pointers.

📢 This Program is part of the Pointers Practice Programs series

let’s look at the example input and output of the program.

Example Input and Output:

Input:

Enter the first number(number1): 6666

Enter the second number(number2): 9999

Output:

Numbers before Swapping - number1:6666, number2:9999

Numbers after Swapping - number1:9999, number2:6666

If you notice, The values of the number1 and number2 are swapped.

Prerequisites:

It is recommended to go through the following pointers and functions tutorials to understand the program better.

Swap Two Numbers using Pointers in C Program Explanation:

Let’s look at the step-by-step explanation of Swapping two numbers using pointers in C programming language.

  1. Start the program by declaring two integer variables, Let’s say they are number1 and number2.
  2. Prompt the user to provide the two numbers and update the input in the number1 and number2 variables respectively. Use the scanf() function to read the data from the user.
  3. Define a function called swap(). The swap() function will take two integer pointers as formal arguments. let’s say they are *number1 and *number2. This function swaps the values pointed by the number1 and number2.
    • Prototype of the swap() function – void swap(int *number1, int *number2)
    • We use the temporary variable to swap the numbers. If you want to swap numbers without using a temporary variable. Please check this article – Swap two integers without using third variable
    • Get the value of the first number by dereferencing the number1 and store it in temp variable – temp = *number1;
    • Copy the number2 value to number1 variable. Again use dereference operator or indirection operator on the pointers to get and update the values. – i.e *number1 = *number2;
    • Finally, Copay the value of the temp variable to number2 variable. – i.e *number2 = temp;
  4. From the main() function, Call the swap() function and pass the number1 and number2 variables with their addresses. use the address of operator to pass the address of the number1and number2.
  5. Finally, Display results on the console.

Program to Swap Two Numbers using Pointers in C Language:

Let’s convert the above program into the c program. Here is the C program to swap two numbers using pointers.

📢 As we are passing the number1 and number2 with the addresses/references, All the changes made to number1 and number2 will be visible in the main() function. That is the reason the number1 and number2 values are swapped.

Swap Two Numbers using Pointers in C – Program Output:

Let’s compile and run the program using GCC compiler (Any compiler).

Test 1:

swap-two-numbers-using-pointers-in-c-program-output

In the above example, The input numbers are 10 and 20. The program swapped the numbers and printed the result on the console.

Test 2:

In this example, The number1 is -399 and number2 is -9000. After swapping the number1 value is -9000 and number2 value is -399.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] Swap Two Numbers using Pointers in C Language […]

Leave a Reply