473,499 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assiciativity

Hi All,

I read somewhere that there is no uniform associativity in C.
What is the author trying to convey? First of all I don't understand
the meaning of assiciativity. Please help.

Thanks,
Mohan.
Nov 14 '05 #1
10 1303
mo************@msn.com (Mohanasundaram) writes:
I read somewhere that there is no uniform associativity in C.
What is the author trying to convey? First of all I don't understand
the meaning of assiciativity. Please help.


Associativity describes how subexpressions involving two or more
operators of same precedence are grouped. For example, the expression

a - b + c

behaves as

(a - b) + c

which is called left-to-right associativity, while

a = b = c

behaves as

a = (b = c)

which is called right-to-left associativity. In C, unary prefix
operators, the ternary conditional operator ?: and assignment operators
associate right-to-left, all other associate left-to-right.

Martin
Nov 14 '05 #2

"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:c0*************@news.t-online.com...
mo************@msn.com (Mohanasundaram) writes:
I read somewhere that there is no uniform associativity in C.
What is the author trying to convey? First of all I don't understand
the meaning of assiciativity. Please help.


Associativity describes how subexpressions involving two or more
operators of same precedence are grouped. For example, the expression

a - b + c

behaves as

(a - b) + c

which is called left-to-right associativity


Yes, but it would be true to say right-to-left here as well. There is
allways right-to-left associativity in C to my understanding.
Nov 14 '05 #3
"Martin Johansen" <ma******@is.online.no> writes:
"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:c0*************@news.t-online.com...
mo************@msn.com (Mohanasundaram) writes:
> I read somewhere that there is no uniform associativity in C.
> What is the author trying to convey? First of all I don't understand
> the meaning of assiciativity. Please help.


Associativity describes how subexpressions involving two or more
operators of same precedence are grouped. For example, the expression

a - b + c

behaves as

(a - b) + c

which is called left-to-right associativity


Yes, but it would be true to say right-to-left here as well. There is
allways right-to-left associativity in C to my understanding.


I don't understand what you mean by that - care to elaborate? The
associativity cannot be left-to-right and right-to-left at the same
time.

Here's an example where it makes a difference:

INT_MAX - INT_MAX + 42

This expression *must* behave like

(INT_MAX - INT_MAX) + 42

i.e. yield 42, on every conforming implementation. OTOH,

INT_MAX - (INT_MAX + 42)

would overflow an intermediate result and thereby cause undefined
behavior.

Martin
Nov 14 '05 #4
"Martin Johansen" <ma******@is.online.no> wrote:

"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:c0*************@news.t-online.com...
mo************@msn.com (Mohanasundaram) writes:
I read somewhere that there is no uniform associativity in C.
What is the author trying to convey? First of all I don't understand
the meaning of assiciativity. Please help.


Associativity describes how subexpressions involving two or more
operators of same precedence are grouped. For example, the expression

a - b + c

behaves as

(a - b) + c

which is called left-to-right associativity


Yes, but it would be true to say right-to-left here as well.


No; that would mean that a - b + c could behave as a - (b + c), which it
isn't allowed to.

Richard
Nov 14 '05 #5
Mohanasundaram writes:
I read somewhere that there is no uniform associativity in C.
What is the author trying to convey? First of all I don't understand
the meaning of assiciativity. Please help.


The next time you have a similar problem, try this.

In google type <wikipedia associativity>. Be sure associativity is
underlined, that serves as a spell check. Then click the topmost entry
which will most likely give you a nice definition. You can search Wikipedia
directly too, but it is temperamental.

Oops, this particular word fails the spell test. But most of them pass
.......
Nov 14 '05 #6

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
"Martin Johansen" <ma******@is.online.no> wrote:

"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:c0*************@news.t-online.com...
mo************@msn.com (Mohanasundaram) writes:

> I read somewhere that there is no uniform associativity in C.
> What is the author trying to convey? First of all I don't understand
> the meaning of assiciativity. Please help.

Associativity describes how subexpressions involving two or more
operators of same precedence are grouped. For example, the expression

a - b + c

behaves as

(a - b) + c

which is called left-to-right associativity
Yes, but it would be true to say right-to-left here as well.


No; that would mean that a - b + c could behave as a - (b + c), which it
isn't allowed to.


No, but the error is with you, remember a - b + c means a + (-b) + c which
becomes a + ((-b) + c) which is fully allowed, hence you can allways assume
right-to-left associativity in C.

Richard

Nov 14 '05 #7
Martin Johansen wrote:

No, but the error is with you, remember a - b + c means a + (-b) + c which
becomes a + ((-b) + c) which is fully allowed,
It is not fully allowed. The result might be different. Overflow for
signed integers is undefined you know.

hence you can allways assume right-to-left associativity in C.


You seem to be wrong.

--
Thomas

Nov 14 '05 #8
On Thu, 12 Feb 2004 14:40:56 +0100, "Martin Johansen"
<ma******@is.online.no> wrote:

"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:c0*************@news.t-online.com...
mo************@msn.com (Mohanasundaram) writes:
> I read somewhere that there is no uniform associativity in C.
> What is the author trying to convey? First of all I don't understand
> the meaning of assiciativity. Please help.


Associativity describes how subexpressions involving two or more
operators of same precedence are grouped. For example, the expression

a - b + c

behaves as

(a - b) + c

which is called left-to-right associativity


Yes, but it would be true to say right-to-left here as well. There is
allways right-to-left associativity in C to my understanding.


This discussion has some parallels to one I've been involved with in
alt.comp.lang.learn.c-c++ recently (like, now). The focus there has
been on the unary ops, but it may have some bearing on where you may
have picked up the misconception above.

The way I learned, and have been teaching, precedence and
associativity has been to say that the main gaggle of unary ops up at
the top of the table (++, -- *, &, et. al.) have /equal/ precedence,
and that conflicts are resolved by applying right-to-left
associativity.

I've been informed there's /another/ equally valid way to perceive
precedence for those unary ops: to say that postfix operators have
higher precedence than prefix operators, and there's no need for any
concept of "associativity" wrt those operators.

In any case, that doesn't change the fact that left-to-right
associativity is cut-and-dried for the binary arithmetic / logical
operators.
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #9
"Martin Johansen" <ma******@is.online.no> writes:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
"Martin Johansen" <ma******@is.online.no> wrote:
>
> "Martin Dickopp" <ex****************@zero-based.org> wrote in message
> news:c0*************@news.t-online.com...
> > mo************@msn.com (Mohanasundaram) writes:
> >
> > > I read somewhere that there is no uniform associativity in C.
> > > What is the author trying to convey? First of all I don't understand
> > > the meaning of assiciativity. Please help.
> >
> > Associativity describes how subexpressions involving two or more
> > operators of same precedence are grouped. For example, the expression
> >
> > a - b + c
> >
> > behaves as
> >
> > (a - b) + c
> >
> > which is called left-to-right associativity
>
> Yes, but it would be true to say right-to-left here as well.


No; that would mean that a - b + c could behave as a - (b + c), which it
isn't allowed to.


No, but the error is with you, remember a - b + c means a + (-b) + c which
becomes a + ((-b) + c) which is fully allowed, hence you can allways assume
right-to-left associativity in C.


No, a + (-b) + c must behave like (a + (-b)) + c, not like a + ((-b) + c).

Martin
Nov 14 '05 #10
"Martin Johansen" <ma******@is.online.no> wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
"Martin Johansen" <ma******@is.online.no> wrote:
Yes, but it would be true to say right-to-left here as well.


No; that would mean that a - b + c could behave as a - (b + c), which it
isn't allowed to.


No, but the error is with you, remember a - b + c means a + (-b) + c


No, it does not.

Consider, for example, this situation:

INT_MIN is -32768
INT_MAX is 32767

int a=-10;
int b=INT_MIN;
int c=-10;

then

a - b + c == (a - b) + c == 32758 + -10 == 32748,

but

a + (-b) + c == -10 + (- -32768) *kerchunk* causes overflow.

Richard
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.