Skip to content
Home » C program to Swap Variables using a temporary variable

C program to Swap Variables using a temporary variable

Learn about C program to Swap using a temporary variable in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

C program to Swap using a temporary variable

The below C program uses a third variable to swap the given variables.

Source code:

#include<stdio.h>
int main()
{
    int a=10, b=20;
    int c;
    // swapping
    c = a;
    a = b;
    b = c;
    // print result
    printf("a=%d \t b=%d\n",a,b);
    return 0;
}

Output:

a=20 b=10

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C Program to Find the Square Root of a Number
C program to find ASCII value in a given Range