Article View: comp.lang.c
Article #1357Re: Rationales for result type of operations using operators like '+='
From: bartc
Date: Fri, 06 Nov 2026 18:11
Date: Fri, 06 Nov 2026 18:11
69 lines
4535 bytes
4535 bytes
On 06/11/2026 17:53, Janis Papanagnou wrote: > On 2026-11-06 17:26, Dan Cross wrote: >> In article <11ceoa2$85p3$3@dont-email.me>, >> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: >>> I just stumbled about something in "C" where I knew there was behavior >>> that other languages designed differently; specifically the "compound" >>> "and-becomes" operators (+=, -=, *=, /=, %=). >>> >>> I'm basically looking for substantial[*] rationales for two properties; >>> (a) that their result is an rvalue (and not an lvalue), and >>> (b) that their evaluation in cascades has right-associativity. >> >> I'm afraid you're not going to find very much here. Those >> operators were added (to B!) less out of choice and more out of >> necessity; as Dennis Ritchie put it in his C history paper from >> HOPL II: >> >> |After the TMG version of B was working, Thompson rewrote B in >> |itself (a bootstrapping step). During development, he >> |continually struggled against memory limitations: each language >> |addition inflated the compiler so it could barely fit, but each >> |rewrite taking advantage of the feature reduced its size. For >> |example, B introduced generalized assignment operators, >> |using `x=+y` to add `y` to `x`. The notation came from Algol 68 >> |[Wijngaarden 75] via McIlroy, who had incorporated it into his >> |version of TMG. (In B and early C, the operator was spelled >> |`=+` instead of `+=`; this mistake, repaired in 1976, was >> |induced by a seductively easy way of handling the first form in >> |B's lexical analyzer.) >> (https://www.nokia.com/bell-labs/about/dennis-m-ritchie/chist.html) >> >> The implication here is that the mechanism was introduced to >> save space, versus the ceremony of, `i = i + 2;` and similar. >> >> As to your specific questions, why make these Rvalues (or, >> rather, not lvalues) and why right-associative? I suspect to >> match the semantics of an assignment expression, > > Yes, I also think this was probably an underlying intention. And > this, the view as assignment - which is actually the most prominent > outlier in the zoo of [dyadic/"binary"] operators concerning its > right-associativity! - (and as opposed to a normal expression) also > contributes to the confusion some obviously have here and thus > consider another semantics - one that considers these op= as part of > a typical expression that are regularly evaluated from left to right > - as "inconsistent" despite being just the normal way to evaluate > expressions. > >> so that one could write things like: >> >> x = i += j++ * 3 + 5; > > Yes, in "C" they like side-effects. Here we have two! It's strange > that people feel confused about the (simple!) Algol 68 semantics but > have no issues with such expressions. Rewritten without side effects > > i = i + j * 3 + 5; > x = i; > j++; Algol68 can give you side-effects too: x := (i +:= (j +:= 1; j - 1) * 3 + 5); Plus more when you consider that, in a LTR expression, an assignment or compound assignment to X at the start also affects instances of X later on. This is not subjective, it is fact.
Technical Details
Message-ID:
<11cl582$2p3j7$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11ceoa2$85p3$3@dont-email.me> <11ckv3c$d9l$1@reader1.panix.com> <11cl47f$23539$7@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: bartc <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: Rationales for result type of operations using operators like '+=' Date: Fri, 6 Nov 2026 18:11:14 +0000 Organization: A noiseless patient Spider Lines: 69 Message-ID: <11cl582$2p3j7$1@dont-email.me> References: <11ceoa2$85p3$3@dont-email.me> <11ckv3c$d9l$1@reader1.panix.com> <11cl47f$23539$7@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 06 Nov 2026 18:11:14 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2920039"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1975kU7Ig0/KLitiWPr5Mum"; posting-host="06e92dab06e7bbad2d3f6c6728786274" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:FuM7p7O3uhMx1VMSkEYOCqOQk0A= sha256:w+zf5BXIiwBSKQ1PJ+qaBa5q5ACg2f8GI8VoB08d7AM= sha1:6NfpFLfOhGpGTHvg91NZBhOmjP8= sha256:BGzncJTtt0OWwcmiKakXnDtHbqCUBmN+uura4j3vGQM= Content-Language: en-GB In-Reply-To: <11cl47f$23539$7@dont-email.me>