Article View: comp.lang.c
Article #1332Re: Rationales for result type of operations using operators like '+='
From: bartc
Date: Thu, 05 Nov 2026 12:17
Date: Thu, 05 Nov 2026 12:17
77 lines
3948 bytes
3948 bytes
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.
Technical Details
Message-ID:
<11chs5e$1jlqr$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-mailReferences:
<11ceoa2$85p3$3@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 12:17:50 +0000 Organization: A noiseless patient Spider Lines: 77 Message-ID: <11chs5e$1jlqr$1@dont-email.me> References: <11ceoa2$85p3$3@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:17:51 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1693531"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/sdzgVmkTM7M0MPxxIWekq"; posting-host="36d55d013f0977f923261808ebce6f18" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:kRIqV80Rm5C5tfi8J2yf9j4zRyQ= sha256:lk8RaztWtPPjpG41csgmPsHtGRqH+Cv9Rcq62MXqw/U= sha1:IkmmLhEXsAT4nHLPWui2nC2+DVw= sha256:lkxoSIqXyiN+YPmWVHxlOcAdjnrObPJ5+bTPeP/ghTM= Content-Language: en-GB In-Reply-To: <11ceoa2$85p3$3@dont-email.me>