🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1339

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

#1339
From: Richard Harnden
Date: Thu, 05 Nov 2026 14:38
139 lines
6260 bytes
On 05/11/2026 14:31, bartc wrote:
> On 05/11/2026 13:23, fir wrote:
>> bartc pisze:
>>> 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;
>>>      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.
>>>
>>
>> btw it is some question if giving this arithmetic precedence
>> is good option
>>
>> normally arithmetic goes sal yeft to right so you have
>>
>>
>> x=a+b*c  //5 chars
> 
> This is 7 characters in all. And normally it would be:
> 
> x=a+b*c;
> 
> which is 8 characters. Typically it will also be indented:
> 
>      x=a+b*c;
> 
> which is 9 characters with a single hard tab. Some also like to add 
> white space:
> 
>      x = a + b * c;
> 
> which is 15 characters.

So what?  Readability is more important that character count.

> 
>> x=b*c+a  //5 chars
>>
>> versus
>>
>> x=(a+b)*c //7 chars
>> x=b*c+a   //5 chars
>>
>>
>> so the left to right would be shorter
>>
>> im not saying thet the higher * precedence is bad, only worth noticing 
>> its not the only option
> 
> Different precedences for the /arithmetic/ operators are good (ie * / 
> are higher than + -) , because then the meaning aligns with how such 
> expressions are universally understood, even in the real world.
> 
> Having to type () is not an imposition if they are necessary. The 
> character count is not too important (usually identifiers will be longer 
> than single characters, plus see above); the number of tokens might be 
> more relevant, as well as () being shifted.
> 
> For the rest of the operators however C made some questionable choices 
> so that often you have to use () because (1) the less natural and useful 
> choice was made, or (2) to make it clear what's happening because the 
> precedences are obscure, unfamiliar or unintuitive, even if technically 
> not needed.

C follows BODMAS (or PEMDAS) doesn't it?
Nothing wrong with adding extra brackets if it make the intention clearer.

Technical Details
Message-ID: <11ci4e3$1mral$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> <11chvvu$1l3ml$1@dont-email.me> <11ci3vq$1mk0h$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: Richard Harnden <richard.nospam@gmail.invalid>
Newsgroups: comp.lang.c
Subject: Re: Rationales for result type of operations using operators like
 '+='
Date: Thu, 5 Nov 2026 14:38:58 +0000
Organization: A noiseless patient Spider
Lines: 139
Message-ID: <11ci4e3$1mral$1@dont-email.me>
References: <11ceoa2$85p3$3@dont-email.me> <11chs5e$1jlqr$1@dont-email.me>
 <11chvvu$1l3ml$1@dont-email.me> <11ci3vq$1mk0h$1@dont-email.me>
Reply-To: nospam.harnden@invalid.com
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 05 Nov 2026 14:38:59 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1797461"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/ZWVWcmTuth91ldxFKPjIuG2kjwMcDz9Q="; posting-host="ce35baf720fa04954185f1d757b73e29"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Od4BpQQ1+pd+yxAMpGIwWkvBCto= sha256:CkOyuyyfXBBcFdgUNX1JTDE7c71/0rRQvNbNpeOSIvE=
	sha1:GQhUzj04lrjbvWpJBGeVvLqyStM= sha256:TEizBzvb5wMa4i/EtMTPPQWBcDEzJN7asnNjmGBn+T4=
In-Reply-To: <11ci3vq$1mk0h$1@dont-email.me>
Content-Language: en-GB