What is associativity for an operator and why is it important?
10 Answers
For operators, associativity means that when the same operator appears in a row, then which operator occurence we apply first. In the following, let Q
be the operator
a Q b Q c
If Q
is left associative, then it evaluates as
(a Q b) Q c
And if it is right associative, then it evaluates as
a Q (b Q c)
It's important, since it changes the meaning of an expression. Consider the division operator with integer arithmetic, which is left associative
4 / 2 / 3 <=> (4 / 2) / 3 <=> 2 / 3 = 0
If it were right associative, it would evaluate to an undefined expression, since you would divide by zero
4 / 2 / 3 <=> 4 / (2 / 3) <=> 4 / 0 = undefined

- 496,577
- 130
- 894
- 1,212
-
1do you know how to find associativity whether it is left or right for given grammer ? – user2510115 Oct 04 '13 at 15:49
-
2For example `expr -> expr + term;` is left associative and `expr -> term + expr` is right associative. – C-- Apr 21 '14 at 13:12
-
17In the first line of your answer, instead of "when the same operator appears" it is more appropriate to say "when operators of same precedence appears". Example: a * b / c => where * and / have same precedence. – 1O1 Sep 17 '14 at 13:14
-
2@1O1 thanks, but what happens if those operators with the same precedence have different associativity? How would `a * b / c` evaluate if `*` would be left-associative but `/` would be right associative? Then there's a contradiction. So I think one needs to say "when operators with the same precedence and associativity" if you want to cover multiple operators. – Johannes Schaub - litb Jan 07 '17 at 13:58
-
@JohannesSchaub-litb Is it possible for operators with the same precedence to have different associativity? It seems like it'd make things hard or impossible to parse unambiguously. – Mark Apr 08 '17 at 18:11
-
2@Mark i don't know, but I can't think about how it should work. Probably worth an extra stackoverflow question – Johannes Schaub - litb Apr 08 '17 at 20:31
-
Okay, so the same thing in Python (2,3) is `4 // 2 // 3 == 0`, but `4 // (2 // 3)` yields `Traceback (most recent call last): File "
", line 1, in – bjd2385 Nov 07 '17 at 15:36ZeroDivisionError: integer division or modulo by zero `
There are three kinds of associativity:
The Associative property in mathematics
Order of Operations in programming languages
The Associative property in mathematics is a property of operators such as addition (+). This property allows you to rearrange parentheses without changing the value of a statement, i.e.:
(a + b) + c = a + (b + c)
In programming languages, the associativity (or fixity) of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses; i.e. in what order each operator is evaluated. This can differ between programming languages.
In CPU caches, associativity is a method of optimizing performance.

- 178,213
- 47
- 333
- 501
-
4*the associativity (or fixity) of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses* - that phrase was just perfect to make me understand – Rafael Eyng May 28 '17 at 15:54
Simple!!
Left Associative means we evaluate our expression from left to right
Right Associative means we evaluate our expression from right to left
We know *, /, and % have same precedence, but as per associativity, answer may change:
For eg: We have expression: 4 * 8 / 2 % 5
Left associative: (4 * 8) / 2 % 5 ==> (32 / 2) % 5 ==> 16 % 5 ==> 1
Right associative: 4 * 8 /(2 % 5) ==> 4 * ( 8 / 2) ==> 4 * 4 ==> 16

- 667
- 8
- 16
-
2There seems to be an error in the answer: `2 % 5` evaluates to `2`, not `0`. – Caleb Stanford May 15 '20 at 18:56
it is the order of evaluate for operators of the same precedence. The LEFT TO RIGHT or RIGHT TO LEFT order matters. For
3 - 2 - 1
if it is LEFT to RIGHT, then it is
(3 - 2) - 1
and is 0. If it is RIGHT to LEFT, then it is
3 - (2 - 1)
and it is 2. In most languages, we say that the minus operator has a LEFT TO RIGHT associativity.
Update 2020:
The situation about 3 - 2 - 1
might seem trivial, if the claim is, "of course we do it from left to right". But in other cases, such as if done in Ruby or in NodeJS:
$ irb
2.6.3 :001 > 2 ** 3 ** 2
=> 512
The **
is "to the power of" operator. The associativity is from right to left. And it is
2 ** (3 ** 2)
which is 2 ** 9
, i.e., 512
, instead of
(2 ** 3) ** 2
which is 8 ** 2
, i.e., 64
.

- 146,324
- 131
- 460
- 740
It's the order of binding of operands to an operator. Basically:
a - b + c
might be evaluated as (assuming - and + have the same precedence):
((a - b) + c) or,
(a - (b + c))
If operators are left associative (bind immediately to the left operand), it'll be evaluated as the first. If they are right associative, it'll be evaluated as the second.

- 30,738
- 21
- 105
- 131

- 414,610
- 91
- 852
- 789
If you are referring to "operator associativity" - it is how a language determines how operators of the same precedence are grouped in the absence of parentheses.
For example, the + and - operators in C-based languages have the same precedence. When you write an expression that uses both of them (without parentheses) the compiler must determine what order to evaluate them in.
If you write 12 - 5 + 3, the possible evaluations include:
- (12 - 5) + 3 = 10
- 12 - (5 + 3) = 4
Depending on the order you evaluate the expression in, you can get different results. In C-based languages, + and - have left associativity, which means that the expression above would evaluate as the first case.
All language have strongly-defined rules for both precedence and associativity. You can learn more about the rules for C# here. The general concepts of operator associativity and precedence are well covered on wikipedia.

- 129,300
- 32
- 216
- 265
-
Your examples would be clearer if they all used the same operands. – Michael Carman May 30 '09 at 21:33
-
What would happen if two operators with the same precedence appeared in an expression without parantheses, but one of them had left associativity and the other had right? Would it just use the associativity of which ever operator it finds first? – Hector May 24 '16 at 14:48
-
it cant happen as same precedance means same associativity. if this werent the case thaere could be ambiguities that threaten the very existance of reality. – Ankur S Jul 19 '18 at 07:25
If you mean operator associativity:
It defines the way expressions are parsed. It gives a standard, so every expression is parsed the same way.
It's mostly important for operations which have the same precedense, when there could be side effects.

- 99,403
- 23
- 97
- 120
We all know that precedence is important but so is associativity in interpreting the meaning of an expression. For a really simple intro try Power of Operators.
Associativity comes under the order of computation in the programming language concepts. The order of computation determines the meaning of the expression. It has two main rules,
- Precedence rules
- Associativity rules
precedence rules define the order in which "adjacent" operators of different types are evaluated. Every programming language has its own operator precedence table regarding its operators.
Coming back to the associativity,
It defines the order of execution of adjacent operations with the same precedence. It has 3 flavors,
left-associativity
right-associativity
non-associativity
If an operator is left-associative it evaluates from left to right likewise if it is right-associative it evaluates from right to left.

- 591
- 2
- 8
- 26
Most of the previous examples have used constants. If the arguments happen to be function calls, the order that the calls are made in may be determined by the association rules, depending of course on your compiler. And if those functions have side effects ..

- 12,640
- 5
- 49
- 63