Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   associativity of operators in C language (http://www.go4expert.com/forums/associativity-operators-c-language-t5649/)

hitesh_best 3Aug2007 21:00

associativity of operators in C language
 
Hello friends,

First of all I would like to say that although I have initiatiated this thread in "engineering concepts" forum, but I thing it should be here, please forgive me for this.

I am following Let us C, 6th edition, by Yashavant Kanetkar,
I have a 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,

g = 10/5/2/1

Now we have to find the order in whcih the calculations will take place
In his solution book, he gives the solution using a table like this.

Code:

operator            left operand              Right operand                        Remark
   
1st    /              10                        5 or 5/2/1              Left operand is unambiguous,
                                                                                            Right is not

2nd  /              10/5 or 5                  2 or 2/1            Left operand is unambiguous,
                                                                                            Right is not

3rd  /              10/5/2 or 2                1                      Right operand is unambiguous,
                                                                                            Left is not

next he says
"operation will be performed first which has an unambiguous left operand.
evaluation order will be:

10/5/2/1
2/2/1
1/1
1
"

My questions are:

1) 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


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.

3) If two '/' has left operand as unambiguous (assuming that what he is saying in row 2 is correct) , how he solved the problem. I mean how he decided that first operatoe '/' will be evaluated first.

Thanks a lot friends.

shabbir 4Aug2007 12:36

Re: associativity of operators in C language
 
Quote:

Originally Posted by hitesh_best
1) 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

That is also possible but probably what the context of the understanding goes he meant the right operand can be both but he probably did not mean should be both.

Quote:

Originally Posted by hitesh_best
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.

So whats your question here.

Quote:

Originally Posted by hitesh_best
3) If two '/' has left operand as unambiguous (assuming that what he is saying in row 2 is correct) , how he solved the problem. I mean how he decided that first operatoe '/' will be evaluated first.

What he meant is unambiguous operators will be evaluated. when its 10/2/5 Its either the 10/2 = Result/5 is evaluated or 10 / Result (2/5) is done depending on the complier implementation and its named and left unambiguous or right unambiguous.


All times are GMT +5.5. The time now is 14:16.