desertSniper87 desertSniper87 - 1 year ago 81
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download