Below is the program for swapping variables without any extra variable.

#include "stdio.h"
#include "conio.h"
void main()
{
   int a=7;b=5;
   clrscr();
   a=a+b;
   b=a-b;
   a=a-b;
   getch();
}

Edited 6 Years Ago by Nick Evan: Added CODE-tags

a=a+b;
b=a-b;
a=a-b;

Will indeed swap to variables without a temp. Notice that you have to make sure that a+b does not exceed the maximum allowed integer. There is another way of swapping the variables without these constraints:

void swap (int *x, int *y) 
{
   if (x != y) 
   {
      *x ^= *y;
      *y ^= *x;
      *x ^= *y;
   }
}

>> Below is the program for swapping variables without any extra variable.

May I suggest that you view for example this thread ;)

Comments
Yes, thanks for pointing that out!

hai friend
this code also pretty works to swap two numbers without using a temporary variable and also is the code for swapping two numbers using a single line code

void main()
{

int a=5,b=8;
printf("before swap %d  %d",a,b);
b=(a+b)-(a=b); //swapping code
printf("after swap %d %d",a,b);

}

Edited 6 Years Ago by Nick Evan: Added CODE-tags

This question has already been answered. Start a new discussion instead.