From Wikipedia, the free encyclopedia

Totally wrong

Addition and multiplication, by contrast, have no inherent associativity

Wrong.

They are both associative. "Inherent" associativity doesn't really mean anything, does it? What the author was perhaps trying to say is that they are not exclusively left associative, nor exclusively right associative. But, since they are both perfectly associative, saying they "have no inherent associativity" is a very bad way of expressing this.

Phrase Meaning examples, by convention
associative A op B op C = (A op B) op C = A op (B op C) addition, multiplication
left associative A op B op C = (A op B) op C subtraction, addition, multiplication
right associative A op B op C = A op (B op C) exponentiation, addition, multiplication

James.cartwright.21 ( talk)

Untrue statement

The following statement was removed by user 83.21.237.8 with the reason as "not true".

Associativity normally applies only to binary operators (operators with two arguments), written in infix notation, such as +, -, *, /.

What exactly are you disagreeing with?

  • If you disagree with the word binary, then perhaps you (or someone else) could add a section to the article on the associativity of operators with three arguments (like the ?: operator).
  • If you are disagreeing with the word infix, then know that associativity doesn't apply to polish notation (where all operators are prefix operators), or reverse polish notation (where all operators are postfix operators), because neither polish notation, or reverse polish notation is ambiguous.
  • Perhaps you were thinking that unary operators also have an associativity property. If you indeed have a definition for the associativity of a unary operators, perhaps you (or someone else) could add a section to the article to explain that and why it's useful. I would not consider it useful, because I don't think there is any ambiguity with unary operators that cannot be resolved by operator precedence. If you allow a unary operator to be used in both prefix and postfix notation, then yes, that could be ambiguous; e.g. the ++ operator in C: the expression ++x++ could be (++x)++ or ++(x++), but these are not ambiguous in practice because they both mean the same thing. Ambiguity introduced by the interaction between binary and unary operators (e.g. is -1+2 actually (-1)+2 or -(1+2)) is actually resolved by operator precedence and not associativity (unary minus has higher precedence than binary plus and minus).
  • If you are disagreeing with the narrow list of examples (+, -, *, /), then know that that was not intended to be a complete list.

-- Egriffin ( talk) 22:43, 28 March 2008 (UTC) reply

"For example, addition, subtraction, multiplication and division are all left-associative." Um, addition and multiplication can be evaluated left- or right- to the same result. e.g. (2 + 3) + 4 = 2 + (3 + 4) — Preceding unsigned comment added by 67.221.158.49 ( talk) 20:08, 5 October 2012 (UTC) reply

Error

"which sets both a and b to the value of c." This is incorrect. a=b=c sets b to the value of c, and sets a to the value of b=c, which is the value of b. The difference maybe significant where the types of a and b differ. —Preceding unsigned comment added by 216.239.45.4 ( talk) 09:09, 4 February 2009 (UTC) reply

Two kinds of "non-associativity"

Non-associative operators currently talks about two things:

  • a * b * c being illegal
  • a * b * c being legal and meaning something different from (a * b) * c or a * (b * c)

Is the ambiguity of the term taken straight from any credible source? Using the same term for both cases is confusing, as they have nothing in common. There ought to be a term for each case to distinguish them. But can anyone find such terms in the literature? -- Smjg ( talk) 11:40, 3 June 2009 (UTC) reply

Moreover, this in the intro
"Operators may be left-associative, right-associative or non-associative."
is an arbitrary trichotomy on the basis of this ambiguity. There are at least five possible cases:
  • left-associative
  • right-associative
  • associative in the standard mathematical sense, with which way round it's evaluated being left up to the implementation
  • the two I listed above
I therefore feel that the sentence should be rewritten, but I'm not sure how best to word it. -- Smjg ( talk) 13:00, 21 August 2009 (UTC) reply
I agree with the sentiment that this trichotomy is arbitrary, espeically in the light of lack of citations. For what it's worth, there's also "chain" associativity, which is used to make things like 1 < b < 3 behave in a true mathematical fashion instead of something like (1 < b) < 3 which it might otherwise. Rnddim ( talk) 19:47, 26 October 2013 (UTC) reply
There isn't "also" "chain" associativity - it's an example of one of the cases I listed:
  • a * b * c being legal and meaning something different from (a * b) * c or a * (b * c)
But I can see a grey area: a * b * c being nondeterministic. I suppose you would normally consider it another case of this, but if the nondeterminism happens at the parsing or optimisation stage rather than the evaluation stage, or is a case of what's known as undefined behaviour, then it feels a bit different.
Smjg ( talk) 08:23, 28 May 2019 (UTC) reply
Yes, the trichotomy is badly wrong. The worst thing about it is that it excludes the common case of "in the standard mathematical sense" as identified by Smjg above. This sense needs to be added. James.cartwright.21 ( talk)

Associativity and order-of-evaluation

Does the "associativity" of an operator determine which side of the operator is actually evaluated first?

For example, if the "+=" operator is right-to-left associative in C, does that determine how the following statement should behave?

  int a5 = {1, 2, 3, 4, 5};
  int b5 = {10, 20, 30, 40, 50};
  int i = 1;

  a++i = b++i];

This is horribly written code, but situations like this can arise as a result of macro expansions, for example. It compiles without warning or error on most compilers, but I have not found anything in the standard that indicates whether that is equivalent to:

  a2 = b3];

... or ...

  a3 = b2];

- LesPaul75 talk 19:52, 9 June 2009 (UTC) reply

Ah, the spec says that the behavior is undefined. - LesPaul75 talk 20:41, 9 June 2009 (UTC) reply
What you are refering to in this example is not the order in which expressions are evaluated (i.e. their velues calculated), but the order in which their side-effects take place. The side-effects must occur only before the next sequence point, in fact in no particular order. 85.247.94.167 ( talk) 17:29, 26 May 2012 (UTC) reply

you have given the example fo exponentiation operator, and you have also mentioned that the exponentiation operator has right associativity, though in c, there is no operator for exponentiation. —Preceding unsigned comment added by 59.97.219.125 ( talk) 11:33, 27 August 2010 (UTC) reply

This is not an article about some specific programming language... — Preceding unsigned comment added by 83.253.247.136 ( talk) 13:08, 6 October 2012 (UTC) reply
Associativity has nothing to do with order of evaluation. Associativity is syntax. Order of evaluation is semantics.
In your example, there are six steps to the evaluation. Here are the dependencies:
Increment i in a[++i]Determine the memory address
STARTCopy the value acrossEND
Increment i in b[++i]Determine the memory addressExtract the value
Whether = is left-associative or right-associative plays no part in the sequencing of these actions. If you chain assignments (a[++i] = b[++i] = c[++i];) then it becomes:
Increment i in a[++i]Determine the memory address
STARTIncrement i in b[++i]Determine the memory addressCopy the value acrossEND
Copy the value across
Increment i in c[++i]Determine the memory addressExtract the value
OK, so the two value copies can only happen in one order*, but it would be the same if = were defined as left-associative and brackets were used (a[++i] = (b[++i] = c[++i]);).
* That isn't strictly true, as a complier could implement the assignment to a[++i] as a copy directly from c[++i] as the result is the same (in the absence of data type conversions), but that's an aside.
Perhaps a better illustration of the independence of order of evaluation and associativity lies in how the C boolean operators are defined. p && q is defined as "evaluate p and, if true, evaluate q, and return the result as a boolean". Thus (p && q) && r and p && (q && r) yield the same sequence of evaluations of p, q and r. The C standard defines && and || to be left-associative, but it would be the same if they were right-associative, ceteris paribus. The only real reason for picking one is to make the grammar unambiguous. — Smjg ( talk) 01:15, 27 March 2022 (UTC) reply

Right vs. left associativity for ?: operator

I think it might be educational to compare/contrast the ?: operator as it applies to PHP (left associative) vs. pretty much all other languages (right associative). I think it's an interesting and curious example, but it might not be worth including because many people would consider this to be a bug in PHP. Thoughts? ozhu ( talk· contribs) 07:43, 12 July 2013 (UTC) reply

Cs

Question Kumari parru ( talk) 07:56, 29 June 2020 (UTC) reply