🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1329

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

#1329
From: Janis Papanagnou
Date: Thu, 05 Nov 2026 02:19
178 lines
8032 bytes
Thanks for your answer. (I'll keep all your explanations for context.)
My comments below...

On 2026-11-04 10:56, David Brown wrote:
> On 04/11/2026 08:53, Janis Papanagnou 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 don't see a distinction here between compound assignment and simple 
> assignment - the compound assignment operators follow the simple 
> assignment operator for consistency, and because "x += y" is defined to 
> mean "x = x + y".  So I think that we can just talk about simple 
> assignment (or at least, talk about it first).

Okay, I see where you're coming from. - Actually that reasoning is one
of the couple rationales that the AI provided me as answer as well.

Since I don't see the need to not have a distinction between = and op=
I dismissed that. In consequence, as you say or imply, all you derive
from that is thus also not convincing to me. - Let me expand on that...

First allow that I see = and op= as different things. (Note that, e.g.,
Algol 68 also differentiates here, so it's not a given that we handle
them the same or semantically similar.) Allow having, at the design
stage, the option to just define the most sensible characteristics to
allow a consistent yet flexible use without "arbitrary" restrictions.

Given that cascading is considered useful and also used in practice I'd
like to consider that from another perspective; from use-cases.

What use-cases are there that would make it sensible to allow (in "C")
just rvalues, to prevent which misuses, or prevent what ambiguities, or
which problems with compilers, or would introduce any other problem?

What use-cases are there that would make it sensible to right-associate
(as in "C" and C++) modifications of an lvalue.

(Please keep in mind that there's yet no substantial need identified
to consider the two syntactic elements similar.)

I wouldn't think that a C-programmer would have difficulties to confuse
semantics of = and op= (especially given that there's many a lot more
confusing things in "C" as, e.g., the = and == choice). So "consistency"
is as argument vague at best.

I also understand what you wrote below that you'd preferred to not have
the issues in the first place by having "var op= val" being a statement.

(Since "C" is now actually returning a value the question remains why
the two properties - that I formulated as questions - were designed as
they are.)

A design where you can cascade the op= as a pipeline processing appears
quite appealing and natural to me (and I certainly used it; in C++ with
the parenthesis necessary, and in Algol 68 I used it straightforward).

So far on the general view "from outside".

> 
> If you figure out (a), then (b) is the only possible choice.  That is, 
> if you write
> 
>      x = y = z;
> 
> it could not be interpreted as
> 
>      (x = y) = z;
> 
> unless "x = y" were an lvalue.

Assignment with right-associativity I consider natural and all is fine.

> 
> For simple assignment, if "x = y = z" were left associative, and "x = y" 
> were an lvalue, that would mean that instead of being
> 
>      y = z;
>      x = y;
> 
> it would now mean :
> 
>      x = y;
>      x = z;
> 
> which is clearly not a useful construct.

Right, it's not a useful construct for assignments.

> 
> (Personally, I'd rather that assignment had been a kind of statement 
> rather than an expression - but if a language is going to make 
> assignment an expression, it's better that it is a potentially useful one!)

All fine for assignments.

> 
> I think that all covers (b) - why assignment (simple and compound) is 
> right-associative.

And here, as mentioned already, I see the unnecessary (and design-wise
artificial) equalization of the two distinct language atoms, = and op=.

> 
> Then there is (a) - why is the result of an assignment an rvalue, not an 
> lvalue?  And why is it an lvalue in C++ ?
> 
> I don't know an answer to that, other than that I don't think there 
> would be any benefit of making it an lvalue in C.  (In C++, operators 
> can be overloaded and may do other things - making the result an lvalue 
> adds a little flexibility.)

Yes, that's what I also think; it adds flexibility. And avoids an IMO
unnecessary restriction; if there's any side-effect that matters in "C"
I've yet to see one or heard from one. That's why I was asking.

Janis

PS: It might help understanding why it appears natural to me, consider
its semantics "operate [sequentially] on a variable in an expression";
here an example with the two possible Algol 68 syntaxes

   number  TIMESAB base  PLUSAB digit

   number *:= base +:= digit

which in "C" would be

   number *= base;
   number += digit;

or

   number = number * base + digit

and in C++ either one of the "C" forms, or the parenthesized form

   (number *= base) += digit

So, in C/C++, two separate statements, variable duplication, spurious
parenthesis. All adding to the often mentioned "cognitive load".

> 
>> 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?
>>
> 
Technical Details
Message-ID: <11cgp2m$85p3$5@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> <11cevfq$ifq3$1@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: 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:19:02 +0100
Organization: A noiseless patient Spider
Lines: 178
Message-ID: <11cgp2m$85p3$5@dont-email.me>
References: <11ceoa2$85p3$3@dont-email.me> <11cevfq$ifq3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 05 Nov 2026 02:19:03 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="268067"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19S6rp8rM1DpYEcW45V+FmW"; posting-host="bb4bc76d4e37a83d4ff8352de8d1e679"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ykwtIjOJlTNy9Vs9OtZLBXIsfvk= sha256:TtkqbTlGhI0Vw3lkKP093YxHzOeX6pDAvmox+Yo65uY=
	sha1:bop2+HS1t81QieCdQmf9At4rUVc= sha256:/WOl1iaVetedh1qwpIJ3j86X6ZICFNwsyLmEz0JaohY=
Content-Language: en-US
In-Reply-To: <11cevfq$ifq3$1@dont-email.me>