Thread: associativity of operators

  1. #1
    Registered User
    Join Date
    Jul 2007

    associativity of operators

    Hello friends,

    I am following Let us C, 6th edition, by Yashavant Kanetkar,
    I have 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
    In his solution book, he gives the solution.

        operator            left                           Right                         Remark
            /               10                        5 or 5/2/1               Left operand is unambiguous,
                                                                                                Right is not
            /               10/5 or 5                   2 or 2/1                  Left operand is unambiguous,
                                                                                                 Right is not
            /               10/5/2 or 2                1                             Right operand is unambiguous,
                                                                                                  Left is not
    now 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

    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.

    Thanks a lot

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Umeň, Sweden
    Well that's a strange way of explaining it.
    Left-to-Right associativity means basically that the expression is evaluated starting from the left and traversing to the right. The expression 10/5/2/1 is therefore the same as (((10/5)/2)/1), which evaluates to 1. Had it been Right-to-Left it would've been (10/(5/(2/1))) which evaluates to 5 with integer math.
    Wikipedia could help

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    Wow, that's a totally obscure way of explaining it. I'd say forget everything he said.

    Left vs right associativity simply means, if many copies of the same operator are strung in a series, which one happens first? In other words, take this piece of code:

    a + b + c
    Does this mean:

    (a + b) + c
    Or does it mean:

    a + (b + c)
    The first case is "left associative." The second case is "right associative." In reality, addition is left associative. MOST operators are left-associative.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  4. Operators
    By George in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 07:35 PM
  5. Defining operators
    By Lynux-Penguin in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2002, 08:03 AM