When an expression contains two operators of equal priority the tie between them is settled using the associativity of the operators. Associativity can be of two types—Left to Right or Right to Left. Left to Right associativity means that the left operand must be unambiguous. It means it can not be involved in evaluation of any other sub-expression. Similarly, in case of Right to Left associativity the right operand must be unambiguous.

Let us understand this with an example.

EX 1 : Consider the expression a = 3 / 2 * 5 ;

Here there is a tie between operators of same priority, that is between / and *. This tie is settled using the associativity of / and *. But both enjoy Left to Right associativity. The following diagram shows for each operator which operand is unambiguous and which is not.


Since both / and * have L to R associativity and only / has unambiguous left operand (necessary condition for L to R associativity) it is performed earlier.

EX 2 : Consider one more expression a = b = 3 ;

Here both assignment operators have the same priority and same associativity (Right to Left). The following Figure shows for each operator which operand is unambiguous and which is not.


Since both = have R to L associativity and only the second = has unambiguous right operand (necessary condition for R to L associativity) the second = is performed earlier.

EX 3 : Consider yet another expression z = a * b + c / d ;

I request you to share this page on your favorite social book marking site with the below link.

Here * and / enjoys same priority and same associativity (Left to Right). The below Figure shows for each operator which operand is unambiguous and which is not.


Here since left operands for both operators are unambiguous Compiler is free to perform * or / operation as per its convenience since no matter which is performed earlier the result would be same.
54.1

The previous post of the blog deals with Priority of mathematical operators in C Programming.

Related Posts :

Instructions to write a good C Program
Variables in C Programming
Constants in C programming

You can browse COMPLETE C PROGRAMMING COURSE here.

Thank you for visiting PROGRAMMING BLOG. If you liked the post, please subscribe to my blog via email or RSS FEED.You can contact me here for any specific feed back .

I am requesting your's feedback.

Are you satisfied with the present post ?
Have you got sufficient information you are looking for here at this post and on the blog ?
What else is missing and what topics have to be incorporated in this blog ?
Let me know so that i can make it better .

COMMENT HERE and thank you for sparing your valuable time.

I will be very glad if you share this page on your social book marking site with the below link.

Share/Save/Bookmark

0 comments

Post a Comment