Swapping two variables in C

@gagana (757)
India
January 12, 2007 8:40am CST
Can u swap two variables without using a temporaray variable
1 person likes this
15 responses
• United States
12 Jan 07
Yes.. Its very much possible.. You people dont know that.. Too bad.. Ok I will show you how to do it.. Let a and b be two integers. then the following code will work. int a , b; a=5; b=10; //now a = 5 and b = 10 a=a+b; //now a = 15 and b =10 b=a-b; //now a=15 and b = 5 a=a-b; //now a = 10 and b =5 which is the required output.. :) such an easy answer... njoy
@lepunk (230)
• Hungary
13 Jan 07
wow! thats great...never thought its possible :D
@gagana (757)
• India
12 Jan 07
wow its right thanks for ur response
@hobohobo (678)
• Indonesia
13 Jan 07
wow before i read your response i think it's impossible to switch 2 value without a temporary variable, but now i know it can be done, thank for the information. But I think it's only work if the variable is number, isn't it ?
1 person likes this
@amgupta (274)
• India
14 Jan 07
here is one more answer int x,y; x ^= y ^= x ^= y; ^ is XOR bit wise operation execution is left to right...
1 person likes this
@gagana (757)
• India
14 Jan 07
wow its a very tricky answer
1 person likes this
@amgupta (274)
• India
15 Jan 07
thanks dude...
1 person likes this
• India
18 Jan 07
that's nice.. but also tell us the logic behind it...
@lepunk (230)
• Hungary
12 Jan 07
i dont think its possible...maybe with writing the memory directly...however its the same with using a temp. variable
1 person likes this
@gagana (757)
• India
12 Jan 07
no i dont think so actually we can swap two variables withot temporary variable
1 person likes this
@lepunk (230)
• Hungary
12 Jan 07
please post an example code...i'm curious
1 person likes this
@dholey (1383)
• India
20 Jan 07
HEY gagan all the codes mentioned here may have erroneous performance ... to see why see my post on page no 2 and if you fnd it perfect mark it as the best response, kindly check it ... and update ....
@bevanamit (645)
• India
13 Jan 07
no u cannot swap widout using temp variable
@dholey (1383)
• India
20 Jan 07
bevanamit is CORRECT and you can see why in my responce given in page no 2.
@gagana (757)
• India
13 Jan 07
we can... the answer has been already given
1 person likes this
@abhax123 (1695)
• India
14 Jan 07
Its simple a=10 b=5 a=a+b; 10+5; b=a-b; 15-5=10; a=a-b; 15-5=5; there you go
• United States
15 Jan 07
Copy Cat.. Try out something yourself.. See my post above.. njoy
• India
12 Jan 07
Hi, It is very easy. I m writting an example in C. If you can not understand then please let me know. int main() { int i = 5; int j = 3; i = i + j; j = i - j; i = i - j; return 0; }
1 person likes this
• India
18 Jan 07
hmm.. my mistake .... but i will give one more solution for the same... :)
@anup12 (4177)
• India
18 Jan 07
It was a very good discussion.It is because of this discussion we came to know that it is possible.
@dholey (1383)
• India
20 Jan 07
we can swap to variables without using the third variables as codes are already given (using arithmetical or using bitwise XOR operator) but the thing i want to say is , they are not the standard one THEY CAN NOT BE USE AS AN ALTERNATIVE FOR SWAPPING USING THIRD VARIABLE. SO THERE IS NO OTHER CORRECT VERSION FOR SWAPPING TWO VARIABLES WITHOUT USING THIRD VARIABLE , i ams now explaining, do one thing as all of you must have gone throug both codes given here in discussions just try to swap given two variable with given value using those to codes you will come to the conclusion x=-5; y=10; TRY IT YOUR SELF,,,,,, and mark my response as the best response....
@AvizWorld (265)
• India
12 Jan 07
int a= 10, b= 5; printf("a value %d", b); printf("b value %d", a);
• United States
12 Jan 07
very wise.. dont be too wise like this buddy :)
1 person likes this
• India
14 Jan 07
ya u can do that see this code a = a + b; b = a + c; b = a-c some thing like that try it is possible.
• India
13 Jan 07
its easy a,b are variables then just a=a+b; b=a-b; a=a-b;
• India
14 Jan 07
ya u wrote right can u tell me which is the fast loop for , while or do while.
@rohand (31)
• India
14 Jan 07
void main() { int a,b; coutab; swap(a,b); } Swap(int x,int y) { x=x+y; y=x-y;//to swap y x=x-y;//to swap x }
@flikkenni (537)
• Indonesia
12 Jan 07
i am not sure if i can swapping two variables without using temp variable in C. you know variables declared the address of the memory, or a variable has its own place in the memory. so if you swapped it without using temporary variables one of them will be overwrite with the other ones. so i think you need a temporary variables to swap variables in C.
• United States
16 Jan 07
Well that was a good but childish example bro.For loop is the fastest loop.keep posting nice topics.
• United States
12 Jan 07
I agree with the first poster, I'm pretty sure its not possible. Even when you program in assembly or machine code it still requires a temporary variable so I don't see why C would be any different.