C Primer Plus (5th Edition)

   

If you look at the precedence of the multiplication and division operators in Table 7.1, you will find them to have equal precedence. Now, consider the following expression:

How does C# determine which operator is applied first? It cannot get any help from the precedence rules because the two operators have identical precedence. C# then resorts to the rules of associativity. An operator can either have left-to-right or right-to-left associativity. When two operators (such as (/) and (*)) with the same precedence can be applied to the same operand (such as 5 in the previous expression), left-to-right associativity will cause the leftmost operator to be applied first; whereas right-to-left associativity will cause the rightmost operator to be applied first. According to Table 7.1, all the binary operators presented here have left-to-right associativity. Consequently, C# will apply the division operator first in the previous expression, returning a result of 40.


   

Категории