Operator Associativity

Operator Associativity

In programming languages and mathematical notation, 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. If an operand is both preceded and followed by operators (for example, "^ 4 ^"), and those operators have equal precedence, then the operand may be used as input to two different operations (i.e. the two operations indicated by the two operators). The choice of which operations to apply the operand to, is determined by the "associativity" of the operators. Operators may be left-associative (meaning the operations are grouped from the left), right-associative (meaning the operations are grouped from the right) or non-associative (meaning there is no defined grouping). The associativity and precedence of an operator is a part of the definition of the programming language; different programming languages may have different associativity and precedence for the same operator symbol.

Consider the expression a ~ b ~ c. If the operator ~ has left associativity, this expression would be interpreted as (a ~ b) ~ c and evaluated left-to-right. If the operator has right associativity, the expression would be interpreted as a ~ (b ~ c) and evaluated right-to-left. If the operator is non-associative, the expression might be a syntax error, or it might have some special meaning. For example, addition, subtraction, multiplication and division are all left-associative.

Many programming language manuals provide a table of operator precedence and associativity; see, for example, the table for C and C++.

Read more about Operator Associativity:  Examples, Right-associativity of Assignment Operators, Non-associative Operators