Swapping without using a third variable in C

This program will swap the value of these two numbers without using a temporary variable. The idea behind this concept is very simple. First we will sum the two given numbers and store it in a. Then subtract b from a and store it in b, Again subtract b from a and store it in a. Thats all the numbers are swapped

Source code :

#include <stdio.h>

int main(){
    
    int a,b;
    a=10;
    b=20;
    
    printf("First value is %d Second value is %d",a,b );
    
    a=a+b;
    b=a-b;
    a=a-b;
    
    printf("== After Swapping ==");
    printf("First value is %d Second value is %d",a,b );
	
    return 0;
    
}

Output :

First value is 10
Second value is 20
== After Swapping ==
First value is 20
Second value is 10

Output :


                

Comments :