Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C
# include <stdio.h>
int main()
{
	int a,b;
	scanf("%d%d",&a,&b);
	b=(a+b)-(a=b);
	printf("after swapping: a is %d, b is %d", a,b);
	return 1;
}



warning: operation on 'a' may be undefined [-Wsequence-point]

Can anyone provide solution without warning without changing logic.
Posted
Updated 29-Jan-16 3:17am
v5
Comments
Philippe Mori 29-Jan-16 22:00pm    
Since it is undefined, compiler are not required to give a specific answer in such cases. Usually, it is possible to guess probable answers for most compilers.

In that case, the problem is that the compiler is free to execute a = b first in which case a and b would then be both equal to b. Then doing the rest of the equation, then both a and b would have the initial value of b.

In particular, the warning indicate that the problem is about sequence point. You might Google about that for more information. In a few words sequence points would be point where the compiler must perform some operation before and some after the sequence point.

"Can anyone provide solution without warning without changing logic."
No.
Because what you are doing is using a side effect, and the results of the calculation are undefined - they depend on what the compiler does with the execution order. And since with "normal" math is doesn't matter if you evaluate x or y first in "x - y" the compiler is at liberty to work out "y" before it works out "x". If it does, then the calculation that eventually gets done is b = b because (a = b) is executed first, then (a + b), then the subtraction.

It gets worse as well: the compiler is at liberty to change what occurs as a result of optimisations: so what works in development may not work in production...

The C specification allows the compiler writer complete freedom, but other similar languages do either define the execution order as left-to-right or right-to-left which means that this code can't be trusted to work the same even if the code is identical!

Don't do things like this: they are hard to read, compiler dependant, unreliable, and very difficult to maintain.
 
Share this answer
 
Comments
[no name] 23-Jan-16 8:47am    
A 5 for this.
Andreas Gieriet 29-Jan-16 9:49am    
My 5!
Cheers
Andi
As Griff said: Don't do such games in regular code! They are hard to read and maintain. And your example only works by chance and is compiler dependent.

Just for the fun of solving the puzzle: Here is a version that does work and fits in a single line of C-code:
C++
a = a^b, b=a^b, a=a^b;

That is not compiler dependent. But still nobody would normally guess what this line is supposed to do. So, I would never use that little "trick" in production code, but use a temporary variable instead. That by the way might also be slightly faster in many cases as the compiler will most likely put the temporary in a register and then use three simple register copies instead of XORs. (I have never done timings on that, though. It isn't worth the effort.)

In C++ you would of course simply use
C++
std::swap (a, b);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Jan-16 11:28am    
This is a well-known the exercise the student should solve independently and well-known correct solution. My 5.
The solution with +- arithmetic is bad and not universal enough.
—SA
Andreas Gieriet 29-Jan-16 9:49am    
My 5, too!
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900