C Program to Swap Two Numbers

In this tutorial, we will learn how to swap two numbers in C program. Before that, if you may go through the following topics in C that are used in this program.


C Program to Swap Two Numbers using temporary variable

The program takes two number inputs from the user that needed to be swapped. And using a third variable, we swap the value. The third variable used is named temp in the program.

#include<stdio.h>

int main()
{
    int a, b, temp;
    
    printf("Enter a: ");
    scanf("%d", &a);
    
    printf("Enter b: ");
    scanf("%d", &b);
    
    temp = a;
    a = b;
    b = temp;
    
    printf("The swapped values a: %d and b: %d", a, b);
    
    return 0;
}

Output:

Enter a: 10
Enter b: 20
The swapped values a: 20 and b: 10

You may want to check the following swapping program in C.