![]() |
Swapping logic
#include<stdio.h>
/*new logic for swapping*/ #include<conio.h> void main() { int x=20,y=30; printf("nos before swapping are %d and %d\n",x,y); x=x*y/(y=x); printf("nos after swapping are %d and %d\n",x,y); getch(); } |
Re: Swapping logic
On 9/23/12 8:48 AM, ZUBAIR TRALI wrote:
> #include<stdio.h> > /*new logic for swapping*/ > #include<conio.h> > void main() > { > int x=20,y=30; > printf("nos before swapping are %d and %d\n",x,y); > x=x*y/(y=x); > printf("nos after swapping are %d and %d\n",x,y); > getch(); > } > This invokes undefined behavior, as you set the value of y, and also use the value of y (for a purpose other than getting the value to set y to). The language does not promise that the sub expression x*y will be executed before the sub expression y=x. Also, x*y needs to fit withing the range of an int, so it will only work for "small" numbers, and it fails if y == 0 (as 0/0 again invokes undefined behavior). |
Re: Swapping logic
On 9/23/2012 8:48 AM, ZUBAIR TRALI wrote:
> #include<stdio.h> > /*new logic for swapping*/ > #include<conio.h> > void main() > { > int x=20,y=30; > printf("nos before swapping are %d and %d\n",x,y); > x=x*y/(y=x); > printf("nos after swapping are %d and %d\n",x,y); > getch(); > } Wonderful![*] Splendid![*] You're a genius![*] Unfortunately, my stupid computer misbehaves when I use your brilliant technique with x=0. It even misbehaves when both x and y are 50000 to start with (which ought to be trivial to "swap"). Also, my compiler complains foo.c:6: warning: operation on 'y' may be undefined What am I[*] doing wrong? [*] <http://dictionary.reference.com/browse/irony> -- Eric Sosman esosman@ieee-dot-org.invalid |
Re: Swapping logic
On 2012-09-23 07:48, ZUBAIR TRALI wrote:
> #include<stdio.h> > /*new logic for swapping*/ > #include<conio.h> > void main() > { > int x=20,y=30; > printf("nos before swapping are %d and %d\n",x,y); > x=x*y/(y=x); > printf("nos after swapping are %d and %d\n",x,y); > getch(); > } > I think you're looking for: x ^= y; y ^= x; x ^= y; |
Re: Swapping logic
On 2012-09-23, ZUBAIR TRALI <bhat.zubair789@gmail.com> wrote:
> #include<stdio.h> > /*new logic for swapping*/ > #include<conio.h> > void main() > { > int x=20,y=30; > printf("nos before swapping are %d and %d\n",x,y); > x=x*y/(y=x); The problem here is that you're accessing y in an expression where y is modified. That is undefined behavior. More precisely, your accessing y for a purpose other than determining a new value to be stored into y. The following also accesses y in an expression where it is modified: y = y + 1; However, the access in "y + 1" is a necessary operand for computing the value stored in the assignment. This imposes an order, which makes it well defined. This one is not defined: y + (y = 3); The y on the left of the + is accessed, but that access is not necessary for the y = 3 assignment on the right. Because it is not necessary, it does not have to be ordered relative to the assignment, and so we do not know when that access occurs: before, during or after the completion of assignment to y? If you write such ambiguous expressions in your C programs, they will be nonportable between compilers and possibly unreliable even on one compiler. For instance, if you change some code-generation options such as optimization settings, the behavior might change. |
Re: Swapping logic
On 9/23/2012 12:35 PM, Casey Carter wrote:
> On 2012-09-23 07:48, ZUBAIR TRALI wrote: >> #include<stdio.h> >> /*new logic for swapping*/ >> #include<conio.h> >> void main() >> { >> int x=20,y=30; >> printf("nos before swapping are %d and %d\n",x,y); >> x=x*y/(y=x); >> printf("nos after swapping are %d and %d\n",x,y); >> getch(); >> } >> > I think you're looking for: > x ^= y; > y ^= x; > x ^= y; Don't do this. See Question 20.15c on the comp.lang.c Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. -- Eric Sosman esosman@ieee-dot-org.invalid |
Re: Swapping logic
On Sun, 23 Sep 2012 14:19:51 -0400, Eric Sosman wrote:
>> I think you're looking for: >> x ^= y; >> y ^= x; >> x ^= y; > > Don't do this. See Question 20.15c on the comp.lang.c > Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. And especially don't do this (or equivalent): *p ^= *q; *q ^= *p; *p ^= *q; as it's equivalent to *p=0 if p==q. |
Re: Swapping logic
Eric Sosman wrote:
> Wonderful![*] Splendid![*] You're a genius![*] YHBT. |
Re: Swapping logic
On 2012-09-23, Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
>> I think you're looking for: >> x ^= y; >> y ^= x; >> x ^= y; > > Don't do this. See Question 20.15c on the comp.lang.c > Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. Which is of course complete nonsense. It's the kind of thing that happens when somone's _opinion_ that is implictly based on one specific use case gets treated as undisputable and universal fact. The smallest system I've ever used C on had 53 bytes of RAM. You try making the argument that the correct solution is _always_ a temporary variable in that kind of environment. -- Andrew Smallshaw andrews@sdf.lonestar.org |
Re: Swapping logic
On 9/26/2012 1:20 AM, Andrew Smallshaw wrote:
> On 2012-09-23, Eric Sosman <esosman@ieee-dot-org.invalid> wrote: > >>> I think you're looking for: >>> x ^= y; >>> y ^= x; >>> x ^= y; >> >> Don't do this. See Question 20.15c on the comp.lang.c >> Frequently Asked Questions (FAQ) page, <http://www.c-faq.com/>. > > Which is of course complete nonsense.[...] It's people like you who make me glad there are people like Steve Summit. -- Eric Sosman esosman@ieee-dot-org.invalid |
| All times are GMT. The time now is 06:00 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.