🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1334

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

#1334
From: David Brown
Date: Thu, 05 Nov 2026 12:22
85 lines
4438 bytes
On 05/11/2026 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
> 
> So it does not seem to buy you much. But doing it with ordinary 
> assignment has some advantages:
> 
> * Only one assignment is done, not 4 lots of /compound/ assignments.
>    With a poor implementation, that will be less efficient
> 
> * There is less ambiguity, for example:
> 
>    i +:= 2 *:= i;
> 
> The second 'i' will have the new value i + 2, not the original, even 
> though it has not obviously been modified.
> 
> 
>> 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.)
> 
> It is problematical in C too:
> 
>      int      a = 1000;
>      unsigned b = 200;

Just to be clear - you meant "unsigned char b = 200;" here?  Mixing 
types can always risk being confusing or subtle, but I think we agree 
that using multiple assignments (simple or compound) in the same 
expression does nothing to help!

>      int      c = 300;
> 
>      a += b += 100;
> 
> 'a' ends up with the value 1044 rather than 1300. 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.
> 
Technical Details
Message-ID: <11chseq$1jmfe$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-mail
References: <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: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Rationales for result type of operations using operators like
 '+='
Date: Thu, 5 Nov 2026 13:22:50 +0100
Organization: A noiseless patient Spider
Lines: 85
Message-ID: <11chseq$1jmfe$1@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 12:22:51 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1694190"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18Qhqj4um4PcHvyFkNKhBXWSHobxxpsg9A="; posting-host="f6ac7f95d293b258be2951aa29f8872f"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:M3TmEPNWzqFW9JfDeBp498zaDP8= sha256:679WM2AqBMNpi8P46RQIc+J+acMIR4pVNot3hGePlmo=
	sha1:r+9Mj9SGMqnluhme28VPl9rwbS4= sha256:4zwvQPe+C1+Q+8lA3/8hf797LKUg1uhHfKUNMUj53WE=
In-Reply-To: <11chs5e$1jlqr$1@dont-email.me>
Content-Language: en-GB