🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1333

Re: Rationales for result type of operations using operators like '+='

#1333
From: Janis Papanagnou
Date: Thu, 05 Nov 2026 02:58
106 lines
6031 bytes
On 2026-11-05 03:05, Waldek Hebisch wrote:
> 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.
>>
>> It might have been that it appeared not as serious to me because I had
>> mostly used C++, and I may have (wrongly!) assumed that "C" behaves the
>> same way. But sadly, no, they differ here.
>>
>>    i += 2 *= 3 -= 5 %= 11;        // impossible in C and C++
>>    (((i += 2) *= 3) -= 5) %= 11;  // impossible in C, possible in C++
>>
>> But C++ still requires parentheses because of the right-associativity.
>>
>> (I suppose the rvalue to lvalue semantical design variant in C++ was
>> to facilitate such cascades?
>> And the left-associativity was maybe considered to be too drastic a
>> change compared to the (partly) inherited "C" design?
>> But my primary question here is about the "C" design rationale!)
>>
>> Note: My question is not about possible workarounds. (I know them.)
>>
>> Janis
>>
>> [*] I emphasized "substantial" because what the Google AI "explained"
>> to me wasn't at all convincing.
>>
>> PS: Algol 68 (for example) had also a left-associativity which allows
>>    i +:= 2 *:= 3 -:= 5 %*:= 11
>> and given that the "C" authors knew that language but deviated from
>> that paragon there must (or might) have been some thoughts about why
>> it is as it is?
> 
> I think that one should look at the context.  In the period when
> C was born there were several attempts to define higher level or
> portable assembly languages.  If you look at say PL360 you will
> see that its operations were chosen to directly translate to
> 360 operations.  C is more abstract, but clearly constructs of
> C were motivated by ability to get efficient machine code.
> So various 'op=' operators were intended to correspond to
> operations of typical 2 address machine.  But as Kerningham and
> Ritchie explanied there was second motivation: computing address
> of left hand side may be a complex comutation and 'op=' operators
> allow reuse of addressing computation.  Now, let us look at
> cascade that you want.  You have a seqence of operations on
> the same variable.  If this variable in in machine register,
> then naive translation leads to seqence of 2 address machine
> instructions which is quite efficient.  But writing this
> as a sequence of separate C instructions leads to slightly
> longer C code but identical machine code.  If the variable
> is in memory, then naive translation implies repeated
> loads and stores which is clearly rather inefficient.
> It is much more efficient to keep temporary result in
> machine register and only assign final value and that is
> easily done by writing complex expression and assigning
> final result to variable.  So, the example you gave is
> code that Ritchie did not want.  OTOH when you are computing
> complex expression, then it may be quite useful to store
> values of subexpressions for future use.  That is why it
> is natural to use value of assigment in future computations.
> 
> I think that the above reasonably explains associativity.
> One could still make result to be lvalue, but by the above
> argument lvalue is of limited use and it would require some
> effort to efficiently support main use case, that is rvalue
> case and true lvalue use.  I guess that here simplicty
> won: rvalue was needed and easy to implement, lvalue
> offered little benefits but extra effort.
> 
> I do not know if this is substantial enough for you, but
> for me it looks quite consistent with other early decisions
> in C developement.

Don't worry, all (non-AI) views are welcome. Thank you.

Actually I also thought about mappings onto 2-address machine
instructions, but I didn't see any problems with that. Say,

   a *= b += c;       // with left-associativity; not "C"!

would have been transformed to something like

   MULT a, b
   ADD  a, c

or even with 1-address instructions and an ACC register like

   LOAD a
   MULT b
   ADD c
   STORE a

But anyway, I'd have also thought that mapping such simple
expressions onto (any) assembly code would be trivial. (And
I'm not even starting to talk about typical optimizations;
which might not have been that common in K&R times.)

Thanks again for your thoughts.

Janis
Technical Details
Message-ID: <11cgrcd$85p3$7@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-mail
References: <11ceoa2$85p3$3@dont-email.me> <11cgoa0$2grfb$1@paganini.bofh.team>

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: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.lang.c
Subject: Re: Rationales for result type of operations using operators like
 '+='
Date: Thu, 5 Nov 2026 03:58:21 +0100
Organization: A noiseless patient Spider
Lines: 106
Message-ID: <11cgrcd$85p3$7@dont-email.me>
References: <11ceoa2$85p3$3@dont-email.me>
 <11cgoa0$2grfb$1@paganini.bofh.team>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 05 Nov 2026 02:58:22 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="268067"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+oDYkmicOWRQRs6DlwL9lg"; posting-host="bb4bc76d4e37a83d4ff8352de8d1e679"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ykPfyGxage+8cR7nWlG4vBquYkI= sha256:/oxBCuquqkQ5gDvbQqzLw69jx+1BnljyhYIH1d6+pm0=
	sha1:J/5Sigwc1FbpG+yhzaeOlNbooHU= sha256:e2PDoCHIN5RYXPNGLB35dsjk3Pksjoy4AkTyTce+0zk=
In-Reply-To: <11cgoa0$2grfb$1@paganini.bofh.team>
Content-Language: en-US