Another method of swapping is by using pointers. Here is an example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,b=3;
swap(a,b);
printf("%d%d",a,b);
getch();
}
swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf("%d%d",x,y);
}
--- On Mon, 15/2/10, Thomas Hruska <[email protected]> wrote:
From: Thomas Hruska <[email protected]>
Subject: Re: [c-prog] swap two numbers
To: [email protected]
Cc: [email protected]
Date: Monday, 15 February, 2010, 11:40 AM
Dev Kashyap wrote:
> [mod-- Before replying to this post, please read the file
> swapnotemp.html in the html folder of the Files section
> of C-Prog... <http://tech. groups.yahoo. com/group/ c-prog/files>
> --mod PN]
>
> Hi friends!!
>
> i want to know what is the different methods to swap two numbers-
> one is using temp variable.
> second is by using xor
> third is by using + and -.
> plz tell me wheather there is another method to swaping.
>
> Thanks
> With Regards
> Dev kashyap
For those too lazy to log into Yahoo, here's a direct link:
http://f1.grp. yahoofs.com/ v1/4OJ4S_ cXRAttP226dTISG7 fYU4tgouBzcWcpJB
nnDGAOqYfy1Zfiph 9NmRQIEYtHP- fkgmFF5hreGXmCQ2 Yl/html/swapnote mp.html
To summarize: Don't do stupid cutesy tricks. Best case: You will
create broken code that appears to work most of the time. Worst case:
You will get fired. Or murdered - "Write code like the next person who
has to maintain it is a serial killer who knows where you live". There
is exactly ONE correct way to swap two numbers. Occam's razor.
You would be surprised how often this question comes up on this group
and how often we have to correct the many people who reply.
I'm CC'ing this to the teacher who was on this list recently wanting
problems for assignments. Teachers, this is the sort of question to NOT
ask students.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleS oft.com/MyTaskFo cus/
The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
http://in.yahoo.com/
[Non-text portions of this message have been removed]