associativity of operators

Discussion in 'C Programming' started by subramanian100in@yahoo.com, India, Mar 16, 2007.

  1. , India

    , India Guest

    What does "associativity of operators" mean ?

    I am unable to undersatand this from K & R 2nd edition. Kindly explain
    with an example.

    Thanks
    , India, Mar 16, 2007
    #1
    1. Advertising

  2. , India said:

    > What does "associativity of operators" mean ?
    >
    > I am unable to undersatand this from K & R 2nd edition. Kindly explain
    > with an example.



    Consider the following code fragment:

    int x = 4;
    int y = 5;
    int z = 6;
    int r = x - y - z;

    printf("r is %d\n", r);

    What would you expect to be printed?

    Is it:

    (a) -7
    (b) 5
    (c) something else

    If you answered (a), try to work out why (b) is also plausible.
    If you answered (b), try to work out why (a) is also plausible.
    If you answered (c), come back when you're feeling better. :)

    Which answer you get depends on the order in which the subtractions are
    done. 4 - (5 - 6) is different to (4 - 5) - 6. Clearly, precedence
    cannot answer this question, since subtraction obviously has the same
    precedence as subtraction!

    So we need another tool for discriminating between operators at the same
    precedence level. That tool is 'associativity'. Left-to-right
    associativity means "do the stuff on the left first, and use the
    calculated result as input for the stuff on the right", and
    right-to-left associativity means, of course, the opposite.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Mar 16, 2007
    #2
    1. Advertising

  3. On Fri, 16 Mar 2007 07:29:43 +0000, Richard Heathfield
    <> wrote:

    >, India said:
    >
    >> What does "associativity of operators" mean ?
    >>
    >> I am unable to undersatand this from K & R 2nd edition. Kindly explain
    >> with an example.

    >
    >
    >Consider the following code fragment:
    >
    >int x = 4;
    >int y = 5;
    >int z = 6;
    >int r = x - y - z;
    >
    >printf("r is %d\n", r);
    >
    >What would you expect to be printed?
    >
    >Is it:
    >
    >(a) -7
    >(b) 5
    >(c) something else
    >
    >If you answered (a), try to work out why (b) is also plausible.
    >If you answered (b), try to work out why (a) is also plausible.
    >If you answered (c), come back when you're feeling better. :)
    >
    >Which answer you get depends on the order in which the subtractions are
    >done. 4 - (5 - 6) is different to (4 - 5) - 6. Clearly, precedence
    >cannot answer this question, since subtraction obviously has the same
    >precedence as subtraction!
    >
    >So we need another tool for discriminating between operators at the same
    >precedence level. That tool is 'associativity'. Left-to-right
    >associativity means "do the stuff on the left first, and use the
    >calculated result as input for the stuff on the right", and
    >right-to-left associativity means, of course, the opposite.


    Nicely said. However it should be pointed out that in Mathematics
    "associative" ordinarily means that it doesn't matter, i.e, if o is an
    operator and X, Y, Z are elements, then o is associative if

    (X o Y) o Z = X o (Y o Z)

    * and + are associative; - and / are not. Left-to-right associativity
    and right-to-left associativity are conventions for resolving
    unparenthesized expressions.
    Richard Harter, Mar 16, 2007
    #3
  4. "Richard Harter" <> wrote in message
    >
    > Nicely said. However it should be pointed out that in Mathematics
    > "associative" ordinarily means that it doesn't matter, i.e, if o is an
    > operator and X, Y, Z are elements, then o is associative if
    >
    > (X o Y) o Z = X o (Y o Z)
    >
    > * and + are associative; - and / are not. Left-to-right associativity
    > and right-to-left associativity are conventions for resolving
    > unparenthesized expressions.
    >

    And associativity isn't just about notational conventions. It is actually
    interesting to mathematicians.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Mar 16, 2007
    #4
  5. On Fri, 16 Mar 2007 21:50:06 -0000, "Malcolm McLean"
    <> wrote:

    >
    >"Richard Harter" <> wrote in message
    >>
    >> Nicely said. However it should be pointed out that in Mathematics
    >> "associative" ordinarily means that it doesn't matter, i.e, if o is an
    >> operator and X, Y, Z are elements, then o is associative if
    >>
    >> (X o Y) o Z = X o (Y o Z)
    >>
    >> * and + are associative; - and / are not. Left-to-right associativity
    >> and right-to-left associativity are conventions for resolving
    >> unparenthesized expressions.
    >>

    >And associativity isn't just about notational conventions. It is actually
    >interesting to mathematicians.


    Indeed.
    Richard Harter, Mar 16, 2007
    #5
  6. On Mar 16, 3:30 pm, (Richard Harter) wrote:

    > Nicely said. However it should be pointed out that in Mathematics
    > "associative" ordinarily means that it doesn't matter, i.e, if o is an
    > operator and X, Y, Z are elements, then o is associative if
    >
    > (X o Y) o Z = X o (Y o Z)
    >
    > * and + are associative; - and / are not. Left-to-right associativity
    > and right-to-left associativity are conventions for resolving
    > unparenthesized expressions.


    In mathematics, + and * are associative. Especially in floating-point
    arithmetic, they are not (take x = 1e100, y = -1e100, and z = 1, then
    (x + y) + z is not the same as x + (y + z). In C, + and * are left
    associative, so x + y + z is (x + y) + z.
    christian.bau, Mar 17, 2007
    #6
  7. , India

    CBFalconer Guest

    "christian.bau" wrote:
    >

    .... snip ...
    >
    > In mathematics, + and * are associative. Especially in floating-
    > point arithmetic, they are not (take x = 1e100, y = -1e100, and
    > z = 1, then (x + y) + z is not the same as x + (y + z). In C, +
    > and * are left associative, so x + y + z is (x + y) + z.


    Not "in mathematics". For a single example, consider vector
    multiplication. You need to specify the fields involved, rings,
    etc.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 17, 2007
    #7
  8. In article <> writes:
    > "christian.bau" wrote:
    > >

    > ... snip ...
    > >
    > > In mathematics, + and * are associative. Especially in floating-
    > > point arithmetic, they are not (take x = 1e100, y = -1e100, and
    > > z = 1, then (x + y) + z is not the same as x + (y + z). In C, +
    > > and * are left associative, so x + y + z is (x + y) + z.

    >
    > Not "in mathematics". For a single example, consider vector
    > multiplication. You need to specify the fields involved, rings,
    > etc.


    In mathematics those are generally not considered standard multiplications.
    (Inner product and outer product in 3D, and in higher dimension vectors you
    get into tensors when doing outer products.) But '*' is also not associative
    in the sedenions. '*' *is* associative in a ring.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Mar 18, 2007
    #8
  9. On Sun, 18 Mar 2007 00:22:40 GMT, "Dik T. Winter" <>
    wrote:

    >In article <> writes:
    > > "christian.bau" wrote:
    > > >

    > > ... snip ...
    > > >
    > > > In mathematics, + and * are associative. Especially in floating-
    > > > point arithmetic, they are not (take x = 1e100, y = -1e100, and
    > > > z = 1, then (x + y) + z is not the same as x + (y + z). In C, +
    > > > and * are left associative, so x + y + z is (x + y) + z.

    > >
    > > Not "in mathematics". For a single example, consider vector
    > > multiplication. You need to specify the fields involved, rings,
    > > etc.

    >
    >In mathematics those are generally not considered standard multiplications.
    >(Inner product and outer product in 3D, and in higher dimension vectors you
    >get into tensors when doing outer products.) But '*' is also not associative
    >in the sedenions. '*' *is* associative in a ring.


    Likewise the octonians.
    Richard Harter, Mar 18, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor

    associativity

    Mantorok Redgormor, Jan 20, 2004, in forum: C Programming
    Replies:
    2
    Views:
    355
    Barry Schwarz
    Jan 21, 2004
  2. Ashwin  N

    Associativity of unary operators

    Ashwin N, Sep 8, 2006, in forum: Java
    Replies:
    4
    Views:
    1,676
    Stefan Ram
    Sep 9, 2006
  3. maadhuu
    Replies:
    3
    Views:
    341
    maadhuu
    Jul 29, 2005
  4. Replies:
    2
    Views:
    300
    Andrey Tarasevich
    Oct 25, 2006
  5. dspfun

    Associativity of unary C Operators

    dspfun, Jan 1, 2007, in forum: C Programming
    Replies:
    28
    Views:
    859
    CBFalconer
    Jan 5, 2007
Loading...

Share This Page