From Ant Moore · 1996-04-12 00:35 · #75282 in R25C
In reply to Re: Speeding up C by Steve Dyson
-=> Quoting Steve Dyson to Matthew Probert <=-
Ant butts in...
[Cut]
SD> One piece in you missive I would like to raise (or should that be
SD> rase)...
MP> NB: Although it is normal practice for expressions to be evaluated
MP> left to right, the ANSI standard on C does not specify an order of
MP> preference for expression evaluation, and as such you should check
MP> your compiler manual.
SD>
SD> IMHO such practice should be shunned by all programmers of any worth!
SD> To justify this remark I would point out that any such code can cause
SD> such havok and cost so dearly when the target hardware changes or when
SD> the development environment changes.
If you're talking about conditional expressions, then I think that ANSI
does specify that the expression is evaluated from left to right. What isn't
guaranteed, and is therefore useful, is that all terms of the condition
will be evaluated; the compiler will bomb out as soon as the final result
of the condition is known; i.e, the first term of an '&&' is FALSE or the
first term of an '||' is TRUE, then no more is required. This means that
if (kbhit () && 'a' == getch ())
...
is only going to do the getch () if there is a key in the buffer. This is
very useful behaviour.
Granted, if the expression is complicated, then for the sake of readability
then it should be split up. But ANSI doesn't require it.
[Cut]
Cheers!
Ant. (Compu$erve 100556,2571)
... A bank will always lend you money if you can prove you dont need it.
--- LoraBBS-OS/2 v2.40b2+
* Origin: The Rowley Rag (2:2500/76.0)