在像" 10 - 3 - 2"这样的表达式中,很容易理解为什么 - 和+运算符是关联的。为了匹配数学约定,结果有5而不是9。据我所知,关联性是指某些运算符具有相同优先级时的顺序。
但这与一元运营商有什么相关性?我不明白为什么一元运算符具有相关性。
注意:问题是关于一般编程,但如果你必须以语言相关的方式回答,C是首选。
答案 0 :(得分:1)
**Arithmetic Operators** (Left-to-Right)
+ Additive operator (a + b)
- Subtraction operator (a - b)
* Multiplication operator (a * b)
/ Division operator (a / b)
% Remainder operator (a % b)
**Unary operators** (Right-to-Left)
+ Unary plus operator; indicates positive value (numbers are positive without this, however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a boolean
但是当我们考虑一元时,例如:
a = +1
a= -1
a++
a-- etc
您在此处提到10 - 3 - 2
的内容将不会被纳入一元操作。
So the operation will be Left-to-Right. Therefore:
10 - 3 equals 7 then
7 - 2 equals 5
Not as given below (Arithmetic operators always Left-to-Right not Right-to-Left)
3 - 2 = 1 then
10 - 1 = 9 This is absolutely wrong.
有关详细信息,请查看以下参考:
- Precedence and Associativity
- Assignment, Arithmetic, and Unary Operators(我与C语言的联系并不多。但运营商很常见。)
醇>