| | |
exchange 2 varibles withoutb using a third variable
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
It's fairly simple to write:
But personally I don't like it. I don't like the assignment and I don't like teachers who come up with this stuff.
The reason is that this can easily cause undefined behavior. Look at this line:
You should tell your teacher that this is a worthless assignment and that this horror of a code will only teach bad coding habits and major problems if ever used in a real project.
c Syntax (Toggle Plain Text)
int a=5; int b=3; a = a ^ b; // a will contain a xor b = 6 b = a ^ b; // b will receive 6 xor 3 = 5 a = a ^ b; // a will become 6 xor 5 = 3 printf("a=%d b=%d", a,b);
But personally I don't like it. I don't like the assignment and I don't like teachers who come up with this stuff.
The reason is that this can easily cause undefined behavior. Look at this line:
b = a ^ b; What value does the right b have? You would say it has it's old value, but perhaps 'a' was already assigned to 'b' before the xor operation was carried out. What would happen then??You should tell your teacher that this is a worthless assignment and that this horror of a code will only teach bad coding habits and major problems if ever used in a real project.
Last edited by niek_e; Apr 18th, 2008 at 7:21 am.
![]() |
Other Threads in the C Forum
- Previous Thread: pipes: problem - simplified
- Next Thread: any pointers on how to do this
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm command copyanyfile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework ide include initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf openwebfoundation overwrite pause pdf pointer posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads turboc unix urboc user whythiscodecausesegmentationfault win32api windowsapi






