Swapping logic

Z

ZUBAIR TRALI

#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();
}
 
R

Richard Damon

#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).
 
E

Eric Sosman

#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>
 
C

Casey Carter

#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;
 
K

Kaz Kylheku

#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.
 
A

Andrew Smallshaw

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top