🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1328

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

#1328
From: Waldek Hebisch
Date: Thu, 05 Nov 2026 02:05
79 lines
4809 bytes
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.

-- 
                              Waldek Hebisch
Technical Details
Message-ID: <11cgoa0$2grfb$1@paganini.bofh.team>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
References: <11ceoa2$85p3$3@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!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: Rationales for result type of operations using operators like '+='
Date: Thu, 5 Nov 2026 02:05:55 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11cgoa0$2grfb$1@paganini.bofh.team>
References: <11ceoa2$85p3$3@dont-email.me>
Injection-Date: Thu, 5 Nov 2026 02:05:55 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="2649579"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2