Re: Side effects in conditional expressions

August Karlstrom wrote:
On 2010-10-02 14:28, Edward wrote:
It depends. If you're entering the IOCCC, it's acceptable if a little
too readable. Anywhere else, it's asking for trouble. The harder you
have to work to understand the code, the more likely subtle bugs are to
get past you

Exactly. As I have already mentioned in this thread I wouldn't write this kind of code myself. Would you also reject the string copy idiom

while (*t++ = *s++);

with the same motivation?


No, I think that's reasonable (essentially it's describing an LDCPIR, which one would hope the compiler would be clever enough to optimise it into), except that if using the result of an assignment as a condition you should enclose in an extra set of brackets. Typically your compiler should warn you otherwise (as you might have meant == instead). Of course, unless you want to compare to something other than zero, string.h's strcpy() would be more appropriate.
The interesting one, though, is if you want to copy within an array, perhaps even copying between regions which overlap - since then you need to be able to choose whether it LDIRs or LDDRs. In that case you might have something like
to shift down, but
to shift up (of course you'd have to initialise i appropriately).
You have to be careful, though, not to invoke UB with something like that (hence why I've used separate comma expressions to inc/dec i).

"Sanity" is the last refuge of the unimaginative.
visit my useless website --

Relevant Pages

  • Re: Smaller C compiler
    ... > undefined behavior from printf. ... the 8-digit displayed values implies the compiler ... if the result of a conversion is wider than the field ... provided that I don't shift by more than 15 at a time. ...
  • Re: Since when is not having a bug patentable?
    ... Iff the compiler generated the code as claimed then it is defective. ... There is a right shift operator>> in C and it works. ... C is a 40-year-old language designed to run on a PDP-11. ... Martin Richards BCPL which was stripped to ...
  • Re: Variadic functions calling variadic functions with the argument list, HLL bit shifts on LE proce
    ... There's no such thing as a "LE register" on most popular CPU ... shift is done on the value. ... >but one would hope that the shr for shift right instruction would ... The compiler needs to use assembler instructions. ...
  • Re: substring finding problem!
    ... can be a power of two. ... wouldn't use * you would use shift. ... I'm having a little trouble thinking of situations in which I would ... It's a common compiler optimization in fact. ...
  • Re: AES trickery ;-)
    ... >> using it in pseudocode seems bizarre unless you are writing a compiler ... My point though was that I've had little trouble ... This isn't helped by the fact that so many "bad C books" ... Randy Howard ...