5

To swap to integers, A and B, this seems to work in C,

A = A + B - ( B = A);

Why does this work? And if this works in all conditions, can this be used to shorten any other commonly implemented algorithms?

user2864740
  • 60,010
  • 15
  • 145
  • 220
n00b
  • 1,549
  • 2
  • 14
  • 33
  • 3
    It works by coincidence on many implementations, however, the behavior of this code is undefined according to the standard. – Marian Aug 01 '15 at 06:47
  • 4
    Furthermore, it just hides the temporary variable in the `B=A` part. It does not really simplify anything. IMHO it worsens the readability of the code. – D.R. Aug 01 '15 at 06:47
  • 3
    Related: [Why does the expression a = a + b - ( b = a ) give a sequence point warning in c++?](http://stackoverflow.com/questions/13317684/why-does-the-expression-a-a-b-b-a-give-a-sequence-point-warning-in-c), [Why is a = (a+b) - (b=a) a bad choice for swapping two integers?](http://stackoverflow.com/questions/20800684/why-is-a-ab-b-a-a-bad-choice-for-swapping-two-integers). – Martin R Aug 01 '15 at 07:03
  • 1
    Funny title as it suggests dealing with A, B, and C. – meaning-matters Aug 01 '15 at 07:05
  • The normal way to do this: A = A + B; B = A - B; A = A - B; or A = A ^ B; B = A ^ B; A = A ^ B; . On an old two accumulator computer, it saved having to use a memory location to hold a temp value. – rcgldr Aug 01 '15 at 09:06

3 Answers3

6

Actually it invokes undefined behaviour according to standards-

C99 §6.5: “2. Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.”

ameyCU
  • 16,489
  • 2
  • 26
  • 41
2

You should not rely on code like that to work because it relies on the fact that it is processed from left to right.

Any implementation that does not do it like that is going to make it fail.

You should avoid writing code that does not work in all cases on all platforms and is not compiler independent, or relies on things that are not documented nor well defined.

So why does it work if you go from left to right : Suppose A = 1 and B = 3
A = A + B - ( B = A);
A = 1 + 3 - (1) = 3
The assignment of B happens where i left the () Bottom line is that only when processing left to right , the assignment of B to A is happening later.

When processing right to left same example : A = A + B - ( B = A);
A = 1 + 1 - (1) = 1
Now the assignment of B is happening first.

Again, you should not rely on code that is not clear for the reader what is happening, and which is compiler dependent which this is the case here.

Philip Stuyck
  • 7,344
  • 3
  • 28
  • 39
1

This is only "shorter" than the standard/obvious way to do it, in the number of lines/semicolons used.

  • It introduces additional operations which might disallow optimization by the compiler
  • It worsens readability (as mentioned by D.R.)
  • It (ab)uses non-standard behavior (as mentioned by Marion), which might make it fail with some compilers

You should never use it.

Hossein
  • 24,202
  • 35
  • 119
  • 224
hoijui
  • 3,615
  • 2
  • 33
  • 41