Article View: comp.lang.c
Article #1335Re: Rationales for result type of operations using operators like '+='
From: fir
Date: Thu, 05 Nov 2026 13:23
Date: Thu, 05 Nov 2026 13:23
100 lines
4598 bytes
4598 bytes
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 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
Technical Details
Message-ID:
<11chvvu$1l3ml$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> <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: fir <profesor.fir@gmail.com> Newsgroups: comp.lang.c Subject: Re: Rationales for result type of operations using operators like '+=' Date: Thu, 5 Nov 2026 14:23:07 +0100 Organization: A noiseless patient Spider Lines: 100 Message-ID: <11chvvu$1l3ml$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 13:23:11 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1740501"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18NCGqlmxl864ABIZPyzNhsyDbYbrS4be8="; posting-host="5ef8622bda737f17f690dd5aebce4a80" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0 SeaMonkey/2.53.24 Cancel-Lock: sha1:XSSBGKs7eSnoDBV2bDN43Rf4JEI= sha256:PU8Ojr7hTIuhNPSgBomrhg0L+bw//SKBEb+J0/ZynjI= sha1:lFML+mye4uCpwO900NMBAJ4dvz4= sha256:pifp+9JawO0SB7X8zoVBiGnKv0CxUj2bLg99L32ctxI= In-Reply-To: <11chs5e$1jlqr$1@dont-email.me>