C语言 += 和 =+ C 赋值运算符有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41616477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:34:07  来源:igfitidea点击:

What is the difference between += and =+ C assignment operators

cassignment-operatorcompound-assignment

提问by Michael

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.

编辑:我犯了一个错误。我在测试期间使用了错误的输入,这让我认为他们都在做同样的事情。原来它们是两种不同的东西。

+=operator addsrvalue to lvalue

+=运算符右值添加到左值

x += y;
x = x + y;

=+simply assignsrvalue to lvalue

=+简单地右值赋给左值

x =+ y;
x = +y;
x = y;

回答by Keith Thompson

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.

在现代 C 语言中,甚至+=是较古老的 C 语言中,是复合赋值运算符,并被=+解析为两个单独的标记。=+。标点符号可以相邻。

So if you write:

所以如果你写:

x += y;

it's equivalent to

它相当于

x = x + y;

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

除了x它只计算一次(如果它是一个更复杂的表达式,这可能很重要)。

If you write:

如果你写:

x =+ y;

then it's parsed as

然后它被解析为

x = + y;

and the +is a unary plus operator.

并且+是一元加运算符。

Veryearly 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=-yintending 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 veryold software and/or hardware.

非常的C早期版本(绕70年代中期,K&R1的在1978年出版之前)用于化合物分配不同的符号。现代 C 使用的地方+=,早期的 C 使用=+。早期的 C 没有一元运算+符,但它确实有一个一元运算-符,并且使用=-引起了问题;程序员会写x=-y它的意思x = -y,但它被默默地解释为x =- y. 为了避免这个问题,语言在 1975 年和 1978 年之间的某个时间发生了变化。直到 1999 年,我还在使用一个编译器(VMS 上的 VAXC),它会警告 的歧义使用=-,但会使用旧的含义。除非您是玩一些非常旧的软件和/或硬件的业余爱好者,否则现在不必担心。

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

(1975年C参考手册显示旧=-=+等形式的复合赋值运算符。第一版C语言编程由Kernighan和Ritchie,1978年出版的,显示了现代-=+=等,但提到了旧的形式在“时代错误”下。)