Assiciativity

M

Mohanasundaram

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.
 
M

Martin Dickopp

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
 
R

Richard Bos

Martin Johansen said:
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
 
M

Martin Johansen

Martin Dickopp said:
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.
 
M

Martin Dickopp

Martin Johansen said:
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
 
O

osmium

Mohanasundaram said:
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
.......
 
M

Martin Johansen

Richard Bos said:
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.
 
T

Thomas Stegen CES2000

Martin said:
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.
 
L

Leor Zolman

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
(e-mail address removed)
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
 
M

Martin Dickopp

Martin Johansen said:
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
 
R

Richard Bos

Martin Johansen said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,355
Latest member
MavoraTech

Latest Threads

Top