Article View: comp.lang.c
Article #1345Re: Rationales for result type of operations using operators like '+='
From: Janis Papanagnou
Date: Thu, 05 Nov 2026 22:41
Date: Thu, 05 Nov 2026 22:41
151 lines
6410 bytes
6410 bytes
On 2026-11-05 13:17, bartc wrote:
> On 04/11/2026 07: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.
>>
>> 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
>
> Apparently this means:
>
> i := ((i + 2) * 3 - 5) %* 11
Yes, this is one of the workarounds, especially in languages that
don't support what I was using.
>
> So it does not seem to buy you much.
We still have the advantages that I mentioned elsethread. But that
was not my question.
I remind: since they have introduced op= to return a value why did
they do it the way they did. Specifically, what are the use-cases
that they support by their implementation semantics (with rvalues
and right-associativity).
> But doing it with ordinary assignment has some advantages:
>
> * Only one assignment is done, not 4 lots of /compound/ assignments.
This appears to me to be nonsensical given that we are talking about
abstractions in a higher-level language, and that the transformation
to machine instructions need not even result in different code.
> With a poor implementation, that will be less efficient
With poor implementation everything will be less efficient.
(What sort of "arguments" are you spreading!)
>
> * There is less ambiguity, for example:
Ambiguity appears if you make up ambiguous samples unnecessarily, as
you do so often, and as you seem to do here.
>
> i +:= 2 *:= i;
What is that supposed to implement in the first place is the question!
I suggest to explain a sensible task, and then we can choose what the
most appropriate coding pattern in any given language might be.
Upthread I gave such an example with a real application and that makes
use of the clear semantics that, e.g., the Algol 68 construct offers:
number *:= base +:= digit
>
> The second 'i' will have the new value i + 2, not the original, even
> though it has not obviously been modified.
Are you now speaking about a left-associative interpretation?
Anyway, you seem to not see the obvious change of 'i' with '+:=', so
I cannot help you.
>
>
>> 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?
>
> In my projects I don't allow such augmented or compound assignments to
> return any value at all. (It is too confusing, it is fiddly to
> implement, and there are too many surprises.)
I understand that it is confusing to you. I understand that you seem
to consider it to be "fiddly". I understand that you are incapable of
defining a clear semantics so that it won't surprise anyone.
I'm fine with your incapabilities. (No one is perfect.) - FWIW, I've
been using that construct in Algol 68 and (with parentheses) in C++
without any confusion or observing any surprises.
>
> It is problematical in C too:
>
> int a = 1000;
> unsigned b = 200;
> int c = 300;
>
> a += b += 100;
>
> 'a' ends up with the value 1044 rather than 1300.
Huh?
As I read it it would (in "C") produce 1300. And a test confirms that.
> However you also see
> that with ordinary chained assignments:
>
> a = b = c;
>
> Both a and b end up with the value 44. So care is needed even here when
> terms are of mixed types.
Huh?
Are you hallucinating?
To summarize your response: you find it confusing, hard ("fiddly") to
implement in a simple and clear way, and the results of that feature
would anyway surprise you.
Okay, thanks. But all that doesn't surprise me since that's obviously
problems you seem to have.
Janis
Technical Details
Message-ID:
<11cj0n7$85p3$8@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> <11chs5e$1jlqr$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 23:41:43 +0100 Organization: A noiseless patient Spider Lines: 151 Message-ID: <11cj0n7$85p3$8@dont-email.me> References: <11ceoa2$85p3$3@dont-email.me> <11chs5e$1jlqr$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 22:41:44 +0000 (UTC) Injection-Info: dont-email.me; logging-data="268067"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/cm3YFVm4mJNcLBYx0xqBi"; posting-host="bb4bc76d4e37a83d4ff8352de8d1e679" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:kx2G9amnwD1j7tZKmhQkQ0VgCjM= sha256:DjhXkGkaQxbxkWdKGDZXdfwrjW3zIbbhcDokLzr6OQY= sha1:/QBivo1VoL+lygZdrVdjNRtl24s= sha256:8oQiyLTCkbduMQb8DKf2xeWGIGGdZHfVdznEWIZisec= Content-Language: en-US In-Reply-To: <11chs5e$1jlqr$1@dont-email.me>