🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1337

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

#1337
From: bartc
Date: Thu, 05 Nov 2026 14:31
130 lines
5768 bytes
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.

> 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.
Technical Details
Message-ID: <11ci3vq$1mk0h$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>

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: bartc <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: Rationales for result type of operations using operators like
 '+='
Date: Thu, 5 Nov 2026 14:31:23 +0000
Organization: A noiseless patient Spider
Lines: 130
Message-ID: <11ci3vq$1mk0h$1@dont-email.me>
References: <11ceoa2$85p3$3@dont-email.me> <11chs5e$1jlqr$1@dont-email.me>
 <11chvvu$1l3ml$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 14:31:23 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1789969"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18qzV9L3iGHJUKWTDKkzhqR"; posting-host="36d55d013f0977f923261808ebce6f18"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:nMUlyWmIfqHuX/a0ESdTWXWFJno= sha256:2gdMjeVu69+dV8KGE2jCF4/9Xkhg/lBPy0afwjMCxgM=
	sha1:RHEEfllCJM0bcDzc4VyC7knSpqM= sha256:O+GYST1H+lgHfswXjHDQGDF58MJocyCKwMB7YmCzDro=
Content-Language: en-GB
In-Reply-To: <11chvvu$1l3ml$1@dont-email.me>