When does operator associativity take place?

Most programming languages ​​have a priority table and associativity for binary operators. In some cases, associativity matters, for example. (a - b) - c! = a - (b - c).

However, for an associative operator like this &&, this would seem to be irrelevant, however most languages ​​list this as a left associative.

Are there situations where there really is a difference between (a && b) && cand a && (b && c)?

+4
source share
2 answers

I can’t believe that there are so many erroneous (deleted) answers ... maybe I should answer that.

First of all, priority! = Associativity! = Evaluation order .

Now that we have this aside: associativity matters in some cases. For a + b + cimportant, when a, band care floating point numbers, not integers, because rounding errors will accumulate differently depending on how the terms are grouped.

&& || , ( ++, C), - , "" ( ) . ++, && || .

+8

@Mehrdads @answer ( ):

- , , ,

a && b && c

. , , , :

(a && b) && c

a && (b && c)

: .

+2

All Articles