# associativity of operators

• 08-03-2007
hitesh_best
associativity of operators
Hello friends,

I am following Let us C, 6th edition, by Yashavant Kanetkar,
I have problem understanding the concept of associativity of operators,

I tried to find some good material on internet by searching in google but could not find much.

Kanetkar writes,

"Left to Right associativity means that the left operand must be unambiguous. Unambiguous in what sense? It must not be involved in evaluation of any other sub-expression. Similarly in case of Right to Left associativity the right operator must be unambiguous."

I could not understand what it means by "being unambiguous".

There is a expression,

Code:

` g = 10/5/2/1`
In his solution book, he gives the solution.

Code:

```    operator            left                          Right                        Remark             /              10                        5 or 5/2/1              Left operand is unambiguous,                                                                                             Right is not         /              10/5 or 5                  2 or 2/1                  Left operand is unambiguous,                                                                                             Right is not         /              10/5/2 or 2                1                            Right operand is unambiguous,                                                                                               Left is not```
now in first row and right column why he writes only two operands that are possible: 5 or 5/2/1 why also not 5/2

In the second row in the remark column why he says that left operand is unambiguous when there are two operands that are possible i.e. 10/5 or 5.

Thanks a lot
• 08-03-2007
OnionKnight
Well that's a strange way of explaining it.
Left-to-Right associativity means basically that the expression is evaluated starting from the left and traversing to the right. The expression 10/5/2/1 is therefore the same as (((10/5)/2)/1), which evaluates to 1. Had it been Right-to-Left it would've been (10/(5/(2/1))) which evaluates to 5 with integer math.
Wikipedia could help http://en.wikipedia.org/wiki/Associa...-associativity
• 08-03-2007
brewbuck
Wow, that's a totally obscure way of explaining it. I'd say forget everything he said.

Left vs right associativity simply means, if many copies of the same operator are strung in a series, which one happens first? In other words, take this piece of code:

Code:

`a + b + c`
Does this mean:

Code:

`(a + b) + c`
Or does it mean:

Code:

`a + (b + c)`
The first case is "left associative." The second case is "right associative." In reality, addition is left associative. MOST operators are left-associative.