Article View: comp.lang.c
Article #1323Re: Rationales for result type of operations using operators like '+='
From: David Brown
Date: Wed, 04 Nov 2026 09:56
Date: Wed, 04 Nov 2026 09:56
86 lines
4417 bytes
4417 bytes
On 04/11/2026 08: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. > I don't see a distinction here between compound assignment and simple assignment - the compound assignment operators follow the simple assignment operator for consistency, and because "x += y" is defined to mean "x = x + y". So I think that we can just talk about simple assignment (or at least, talk about it first). If you figure out (a), then (b) is the only possible choice. That is, if you write x = y = z; it could not be interpreted as (x = y) = z; unless "x = y" were an lvalue. For simple assignment, if "x = y = z" were left associative, and "x = y" were an lvalue, that would mean that instead of being y = z; x = y; it would now mean : x = y; x = z; which is clearly not a useful construct. (Personally, I'd rather that assignment had been a kind of statement rather than an expression - but if a language is going to make assignment an expression, it's better that it is a potentially useful one!) I think that all covers (b) - why assignment (simple and compound) is right-associative. Then there is (a) - why is the result of an assignment an rvalue, not an lvalue? And why is it an lvalue in C++ ? I don't know an answer to that, other than that I don't think there would be any benefit of making it an lvalue in C. (In C++, operators can be overloaded and may do other things - making the result an lvalue adds a little flexibility.) > 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 > 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? >
Technical Details
Message-ID:
<11cevfq$ifq3$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!2.eu.feeder.erje.net!feeder.erje.net!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!2.eu.feeder.erje.net!feeder.erje.net!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: Wed, 4 Nov 2026 10:56:06 +0100 Organization: A noiseless patient Spider Lines: 86 Message-ID: <11cevfq$ifq3$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: Wed, 04 Nov 2026 09:56:11 +0000 (UTC) Injection-Info: dont-email.me; logging-data="606019"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vWcsnU/qieaJqRDqhsA8BJV4Y2QSIRVs="; posting-host="7be39a0b43be68c9a71bf83cb2723f50" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:d3hIpQs/P3wOly07QV5bwvpdLOg= sha256:odlpGJVCxeYI/Gkz0YEoSE3fgWma052xMvN9GPK4uOI= sha1:GGZv+ZLV3dRHTyuS1qOQ6pKHwz4= sha256:51eNCe1zGk2xFyGwy3dOTsrBo982pAFexdVjn69VnBo= In-Reply-To: <11ceoa2$85p3$3@dont-email.me> Content-Language: en-GB