✓ Link copied to clipboard
C 📅 June 30, 2026 ⏱ 5 min read

C Program to Swap Two Numbers Using Pointers

C Program to Swap Two Numbers Using Pointers

Swapping numbers is something you often do in programming. Using pointers in C makes it super efficient. Imagine you’re swapping values in a function — that’s where pointers come into play. Here's how you can swap two numbers with them.

Prerequisites for the C Program to Swap Two Numbers Using Pointers

Before diving into the code, ensure you're familiar with these concepts:

  • Understanding of int variables
  • Pointers and memory addresses
  • <stdio.h> for input/output operations
  • The concept of functions in C
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Swap Two Numbers Using Pointers

Here's the complete source code for swapping two numbers with pointers:

swap_numbers.cC▶ Run
#include <stdio.h>

void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int a, b;
    "Enter the first number: ";
    scanf("%d", &a);
    "Enter the second number: ";
    scanf("%d", &b);

    swap(&a, &b);

    printf("After swapping, first number = %d\n", a);
    printf("After swapping, second number = %d\n", b);

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Swap Two Numbers Program

When you run this program, you'll see the input and the swapped output like this:

OUTPUT
Enter the first number: 5
Enter the second number: 10
After swapping, first number = 10
After swapping, second number = 5
Output of C Program to Swap Two Numbers Using Pointers
Output of C Program to Swap Two Numbers Using Pointers

Algorithm to Swap Two Numbers Using Pointers in C

Swapping two numbers with pointers involves accessing direct memory locations. Here's the logic step by step.

  1. Read two integers a and b from the user.
  2. Define a swap function that takes two pointers as parameters.
  3. In the swap function, store the value of the first pointer in a temporary variable.
  4. Assign the value pointed to by the second pointer to the first pointer.
  5. Place the temporary variable's value in the location pointed by the second pointer.
  6. Print the swapped values.
flowchart TD
  A([Start]) --> B[/Input a and b/]
  B --> C[Define swap function]
  C --> D[Call swap(&a, &b)]
  D --> E[Inside swap: temp = *x]
  E --> F[*x = *y]
  F --> G[*y = temp]
  G --> H[End of swap function]
  H --> I([Print swapped values])
  I --> J([End])
Flowchart: the algorithm step by step

How the C Program to Swap Two Numbers Using Pointers Works

Let's walk through the code line by line to understand its operation.

  • The program starts by including the standard input-output library, <stdio.h>, to handle I/O operations.
  • The swap function is defined to take two int pointers as arguments. It performs the swapping by using a temporary variable to hold one value while swapping.
  • In the main function, two integer variables, a and b, are declared to store the numbers entered by the user.
  • The printf and scanf functions are used for input/output operations to read the numbers.
  • The swap function is called using the addresses of a and b, allowing the function to modify the original variables.
  • Finally, the program outputs the swapped numbers using printf statements to display the results.
  • The program ends with return 0, indicating successful execution.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What does the swap function use to exchange the values of two variables?

🎉Correct! Pointers allow access to the memory addresses of variables, enabling the swap.
🤔Not quite. Think about how the function accesses the variables' memory locations.

Time and Space Complexity of the C Program to Swap Two Numbers Using Pointers

Analyzing the computational complexity for this program.

Time Complexity: O(1). The swap operation involves a fixed number of steps: storing one value in a temporary variable, then performing two assignments. It doesn't depend on the size of input, so it remains constant.

Space Complexity: O(1). Only three variables are utilized in total: a, b, and temp in the swap function. The space requirement is constant regardless of input size.

⚠️

Common mistake: Forgetting to pass the addresses of variables to the swap function will result in no swapping.

Key takeaway:Pointers give you a strong way to manipulate data directly by accessing memory locations.

FAQs About Swapping Two Numbers in C Using Pointers

Here are some frequently asked questions about this program.

Using pointers lets you modify the variables' memory locations directly in the function.
Without pointers, changes only affect copies of the variables, leaving the originals intact.
Yeah, you can do it with arithmetic or bitwise operations, but a temporary variable is way simpler and clearer.
Nope, you can set numbers directly in the code, but scanf is great for user input.
Functions help recycle code, making your program more modular and easier to maintain.
The & operator grabs the memory address of a variable, giving the function access to the original data.

If you found this program helpful, check out these related programs:

Conclusion

Swapping numbers with pointers gives you a peek into memory manipulation. It's a cool trick. You might wanna check out pointer arithmetic next.

Try swapping with similar tasks in the C compiler, or test what you know with our C language quiz.