What is the difference between += and =+ C assignment operators [duplicate]
Asked Answered
M

1

9

I was wondering if there was a difference between =+ and += (and other assignment operators too). I tried and both did the same thing. So is there a difference or is there a convention? Do both work because my compilers dont check for standarts?

Edit: I made a mistake. I used bad inputs during my testing which led me to thinking they are both doing the same thing. Turns out they are two different things.

+= adds rvalue to lvalue

x += y;
x = x + y;

=+ assigns rvalue to lvalue

x =+ y;
x = +y;
x = y;
Mourning answered 12/1, 2017 at 15:6 Comment(11)
There is no =+ operator in C.Iminourea
@EugeneSh. yes but it works. So should i not use it? It only works by chance in my computer?Mourning
I works as two different operators. Assignment and unary +.Iminourea
Well i=+1 will work indeed...Breeching
=+ is the assignment operator along with the unary + operator while the += is the assignment operator along with the addition operator.Chambliss
ideone.com/1Zgtcx Notice the difference...Iminourea
All compound assignment operators begin with the arithmetic operator (+ in this case) and are then followed by the assignment operator (=). In C, += is a valid compound assignment operator, whereas =+ is not.Harlot
@EugeneSh.: strictly, there is no longer a =+ operator in C. It ceased to be a part of C in the mid-70s. Note that =+, =-, =& can both appear in modern C — even =* if the term following is a pointer. Most of the others can't. However, the meaning is of the two separate operators; the fact that they're touching is immaterial.Paraphrastic
If you have: int main(void) { int i = 2, j = 3; i =+ j; printf("%d\n", i); return 0; }, do you get 3 or 5 printed? Standard C says it should be 3. Even I've never worked with a compiler that gives a different result — the change from the original =+ to += occurred years before I started coding in C.Paraphrastic
surprisingly =+ works . foo =+ bar is same with foo = (+ bar)Impartial
@SunggukLim: There's nothing surprising about that. =+ is two operators, = and +. See my answer for details.Christianly
C
20

In modern C, or even moderately ancient C, += is a compound assignment operator, and =+ is parsed as two separate tokens. = and +. Punctuation tokens are allowed to be adjacent.

So if you write:

x += y;

it's equivalent to

x = x + y;

except that x is only evaluated once (which can matter if it's a more complicated expression).

If you write:

x =+ y;

then it's parsed as

x = + y;

and the + is a unary plus operator.

Very early versions of C (around the mid 1970s, before the publication of K&R1 in 1978) used different symbols for compound assignments. Where modern C uses +=, early C used =+. Early C had no unary + operator, but it did have a unary - operator, and the use of =- caused problems; programmers would write x=-y intending it to mean x = -y, but it was silently interpreted as x =- y. The language was changed some time between 1975 and 1978 to avoid that problem. As late as 1999, I worked with a compiler (VAXC on VMS) that would warn about an ambiguous use of =-, but would use the older meaning. That shouldn't be a concern now unless you're a hobbyist playing with some very old software and/or hardware.

(A 1975 C Reference Manual shows the old =-, =+, et al forms of the compound assignment operators. The first edition of The C Programming Language by Kernighan and Ritchie, published in 1978, shows the modern -=, +=, et al, but mentions the older forms under "Anachronisms".)

Christianly answered 12/1, 2017 at 21:22 Comment(2)
To this day I still put a space after the = so a = -7; is not misinterpreted as a -= 7;.Murrey
@chux-ReinstateMonica So do I, but only because I find it more legible.Christianly

© 2022 - 2024 — McMap. All rights reserved.