desertSniper87 desertSniper87 - 20 days ago 6
C Question

Equal sign/Assignment Operator inside statement

I was browsing github the other day and found this piece of code in C++.

order[i]= order[i]+order[r]-(order[r]=order[i]);


Why is there 2 equal signs in a line of code? I have never seen this before.

Answer

The assignment operator = returns an LVALUE, meaning a value can be further assigned to it.

Further, this is poor coding in general, as it's hideous. The author would be better off doing separate assignments over multiple lines, as it's easier to read, less prone to errors while implementing (as an extension of readability), the optimizer will likely generate similar code in either case, etc. Also, this kind of code often leads to a common duplicate question on SO: sequence points, and why this code is horrible.

Comments