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".)
=+
operator in C. – Iminourea+
. – Iminoureai=+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+
in this case) and are then followed by the assignment operator (=
). In C,+=
is a valid compound assignment operator, whereas=+
is not. – Harlot=+
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. – Paraphrasticint 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=+
works .foo =+ bar
is same withfoo = (+ bar)
– Impartial=+
is two operators,=
and+
. See my answer for details. – Christianly