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;
Michael
  • 577
  • 1
  • 11
  • 21
  • 16
    There is no `=+` operator in C. – Eugene Sh. Jan 12 '17 at 15:07
  • @EugeneSh. yes but it works. So should i not use it? It only works by chance in my computer? – Michael Jan 12 '17 at 15:07
  • 6
    I works as two different operators. Assignment and unary `+`. – Eugene Sh. Jan 12 '17 at 15:08
  • Well `i=+1` will work indeed... – Ari0nhh Jan 12 '17 at 15:08
  • `=+` is the assignment operator along with the unary `+` operator while the `+=` is the assignment operator along with the addition operator. – Spikatrix Jan 12 '17 at 15:09
  • https://ideone.com/1Zgtcx Notice the difference... – Eugene Sh. Jan 12 '17 at 15:11
  • 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. – Dan Jan 12 '17 at 15:12
  • 9
    @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. – Jonathan Leffler Jan 12 '17 at 15:13
  • 2
    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. – Jonathan Leffler Jan 12 '17 at 15:16
  • surprisingly `=+` works . `foo =+ bar` is same with `foo = (+ bar)` – Sungguk Lim Aug 31 '17 at 01:58
  • 1
    @SunggukLim: There's nothing surprising about that. `=+` is two operators, `=` and `+`. See my answer for details. – Keith Thompson May 31 '18 at 17:48

1 Answers1

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".)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631