Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is associativity of operators and why is it important?

What is associativity (for an operator) and why is it important?

Updated: operator associativity

like image 682
nonopolarity Avatar asked May 30 '09 20:05

nonopolarity


People also ask

What is the associativity of the operator?

Associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence. An operator's precedence is meaningful only if other operators with higher or lower precedence are present. Expressions with higher-precedence operators are evaluated first.

What is importance of precedence and associativity in C?

The precedence of operators in C dictates the order in which the operators will be evolved in an expression. Associativity, on the other hand, defines the order in which the operators of the same precedence will be evaluated in an expression.

When evaluating a complicated expression What is the role of associativity why it is important?

When two operators have the same precedence, associativity helps to determine the order of operations. Associativity is the order in which an expression is evaluated that has multiple operators of the same precedence.

What is the associativity in C language?

Operator associativity is used to evaluate the order of operators with equal precedence in an expression. In the C programming language, when an expression contains multiple operators with equal or same precedence, we use associativity to determine the order of evaluation of operators.


1 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 
like image 154
Johannes Schaub - litb Avatar answered Oct 02 '22 19:10

Johannes Schaub - litb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!