tags:

views:

132

answers:

5

How can i decide or understand whether any statement or expression has left to right or right to left associativity?

+3  A: 

=, +=, etc, operators that have '=' in them, is right associated.

All others are left-associated.

Pavel Radzivilovsky
This seems to disagree. http://www.difranco.net/cop2220/op-prec.htm
GMan
Perhaps I miss something, but I don't know how in any of the unary or ternary operators the associativity is relevant.... Well, I accept your claim anyway,
Pavel Radzivilovsky
+2  A: 

Here's a table of C++ operator precedence.

The operator precedence and associativity are the same in C and C++ (modulo operators that don't exist in the former).

Cogwheel - Matthew Orlando
That's C++, not C.
GMan
Aren't the precedence and associativity the same (modulo operators that don't exist in C)?
Cogwheel - Matthew Orlando
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
Cogwheel - Matthew Orlando
@Cogwhere: They are, but a user at a level that they need to ask about precedence can't be guaranteed to know that, so the answer should *say* as much or link to a c specific reference. But in any case, bare links are rotten SO answers. At a minimum summarize what's to be found on the other end.
dmckee
@dmckee Thanks for the feedback. I've edited the post.
Cogwheel - Matthew Orlando
+5  A: 

You may find a nice table here.

GMan
Bang! Hit that one right on the head.
NealB
A: 

Mug it up or google!!

Karthick
This is not helpful or constructive. By way of reference, note that Joel himself wrote ["How do I move the turtle in LOGO?"](http://stackoverflow.com/questions/1003841/how-do-i-move-the-turtle-in-logo) to emphasize that no question is too basic.
dmckee
A: 

Read the standards

EDITED: I don't have the standards myself, but I've found this link supposed to be based on the standard.

Also, qoting Kernigham & Ritchie:

Arithmetic operators associate left to right.

Expressions connected by && or || are evaluated left to right, and evaluation stops as soon as the truth or falsehood of the result is known. Most C programs rely on these properties.

Operators Associativity

() [] -> . left to right

! ~ ++ -- + - * (type) sizeof right to left

* / % left to right

+ - left to right

<< >> left to right

< <= > >= left to right

== != left to right

& left to right

^ left to right

| left to right

&& left to right

|| left to right

?: right to left

= += -= *= /= %= &= ^= |= <<= >>= right to left

, left to right

Unary & +, -, and * have higher precedence than the binary forms.

pcent
Read them to us.
GMan
@GMan, I don't have the standards myself, so I added the alternative information I hav, it might be useul
pcent