LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2005, 04:29 AM   #1
skie_knite007
Member
 
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Rep: Reputation: 15
How to exchange two variables without using a third


Is it possible to exchage two variables without using temp?
(in C)

how can it be done.....!!!!!!!!!!
 
Old 03-11-2005, 04:42 AM   #2
idefinehell
LQ Newbie
 
Registered: Feb 2005
Distribution: FC3
Posts: 24

Rep: Reputation: 15
a,b be the two variables
(I)
temp=a;
a=b;
b=temp;

using an extra variable

and with out using an extra variable
(II)
a=a+b;
b=a-b;
a=a-b;

There r many ways 2 swap values of two variables
Hope this helps :-)
 
Old 03-11-2005, 04:48 AM   #3
nulldevice
LQ Newbie
 
Registered: Feb 2005
Posts: 4

Rep: Reputation: 0
a^=b^=c^=a
but all these will work for integers only. when u say variable it has a larger context

Last edited by nulldevice; 03-11-2005 at 05:00 AM.
 
Old 03-11-2005, 05:09 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by nulldevice
a^=b^=c^=a
But that still uses an extra variable: "c"....

This should work:
Code:
a = a^b;
b = a^b;
a = a^b;
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kmail and Exchange kkempter Linux - Software 0 04-21-2005 11:40 AM
any subsitution for ms-exchange?? yenonn Linux - Software 12 03-16-2004 07:45 PM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM
alternatives to exchange? groovin Linux - Software 7 03-27-2003 02:15 PM
MS Exchange tintobg Linux - Networking 2 06-14-2001 04:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration