We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,085 Members — Technology Publication meets Social Media

# exchange of the integer value with using third variable

``````/* EXCHANGE THE INTEGER VALUE WITHOUT USING THE THIRD VARIABLE */
#include<stdio.h>
void main()
{
int a,b;
printf("enter the two integer variabe \n");
scanf("%d,%d",&a,&b);
//exchange the value of a to b ,b to a without using third variable
a=a+b;
b=a-b;
a=a-b;
printf("%d%d",a,b);
getch();
}

/*EXAMPLE: A=3,B=10
A=A+B;  // A=3+10=13
B=A-B;  //B=3-10=3
A=A-B   //A=13-3=10 */
``````
5
Contributors
11
Replies
2 Days
Discussion Span
9 Months Ago
Last Updated
12
Views
shashikumar s g
Newbie Poster
15 posts since Aug 2012
Reputation Points: 0
Skill Endorsements: 0

Yeah, so? Why would anyone want to do this? It's hard to understand and using a temp variable is clean and understandable.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Skill Endorsements: 37

yes, I compelet agree with u in normal time,but when it comes to memory storage or allocation its better to go in this way,time efficience compare to using the third or temp variable

shashikumar s g
Newbie Poster
15 posts since Aug 2012
Reputation Points: 0
Skill Endorsements: 0

but when it comes to memory storage or allocation its better to go in this way,time efficience compare to using the third or temp variable

Really? Please show us why 4 extra bytes for a temp variable is slower than all that math. What are your sources for this assumption of speed vs memory useage? Document your position.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Skill Endorsements: 37

but when it comes to memory storage or allocation its better to go in this way

Memory footprint is a viable argument, but I'd suggest that if the memory for a variable of type int is too much, you have bigger problems. I'm not sure what you mean by allocation though, as local variables are "allocated" with a simple adjustment of the stack pointer.

time efficience compare to using the third or temp variable

Using a temporary variable is all but guaranteed to be more efficient. This is a case of premature pessimization, where the "clever" optimization trick actually makes the code perform worse than the naive code would by confusing the compiler's optimization routines.

deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822
Skill Endorsements: 57
``````#include <stdio.h>
#include <stdlib.h>
void main()
{
int a,b;
printf("enter two numbers: ");
scanf("%d%d",&a,&b);
printf("before swapping....\n");
printf("A=%d B=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("after swapping...\n");
printf("A=%d B=%d",a,b);
}
``````

here try this man ull get perfect output

rithish
Posting Whiz in Training
268 posts since Apr 2011
Reputation Points: 23
Skill Endorsements: 0

ur trying to swap right???

rithish
Posting Whiz in Training
268 posts since Apr 2011
Reputation Points: 23
Skill Endorsements: 0

Here is another Logic for the same :

``````#include<stdio.h>
#include<stdlib.h>
void main()
{
int a,b;
printf("\nEnter two numbers: ");
scanf("%d %d",&a,&b);
printf("\nBefore swapping :");
printf("\nA=%d \t B=%d",a,b);
a=a*b;
b=a/b;
a=a/b;
printf("\nAfter swapping :");
printf("\nA=%d \t B=%d",a,b);
}
``````
thewebhostingdi
Junior Poster
192 posts since Jun 2009
Reputation Points: 3
Skill Endorsements: 0

I'm shocked nobody has pulled out the XOR trick yet. That's usually the first one that pops up when people try to show off the trivia they've learned that has no practical benefit in the real world.

deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822
Skill Endorsements: 57

When developing in low-level languages, particularly assembly languages, and when a low number of registers or limited memory is available, the use of a temporary location could be restrictive

shashikumar s g
Newbie Poster
15 posts since Aug 2012
Reputation Points: 0
Skill Endorsements: 0

ya,i am swapping

shashikumar s g
Newbie Poster
15 posts since Aug 2012
Reputation Points: 0
Skill Endorsements: 0

When developing in low-level languages, particularly assembly languages

You'll notice that this is the C forum, not the assembly forum.

when a low number of registers or limited memory is available

As I mentioned already, that can be a viable argument in favor of the tricks. But that's a niche area, and those platforms are the ones that don't support stdio.h (which you used in your OP). Further, when it is a viable argument, the caveats are made very clear to discourage use outside of the niche area, and you didn't do that. Rather, you tried to palm this off as a generally applicable and appropriate solution, which it isn't.

deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822