J
Jens Thoms Toerring
You mean that * and / are left-associative. The evaluation order is
undefined.
Mmmm, doesn't mean "left-associative" in this case that the
multiplication must be done before the division? That's at
least my understanding of the term which, of course, can be
quite wrong. But if my understanding is correct then saying
that the "expression is evaluated left to right" can be con-
sidered (as a sentence derived from this, and as that it was
meant) to be ok?
Note that this is a peculiarity of unsigned integer arithmetic, which
is required to be modular.
Yes, that was a additional information I perhaps should have
mentioned.
For signed integers, overflow behavior is
undefined, so the compiler is free to optimize the expression by
assuming that it won't overflow.
Since the behaviour on overflow for signed integers is undefined
I would think that the compiler can do whatever it likes, i.e.
it can optimize but also can do something completely different...
So, if you know that x is always non-negative, and that x * HZ won't
overflow, you can do:
x = (unsigned) (((int) x) * HZ / 1000);
At least gcc seems to be able to optimize this properly.
I wouldn't be prepared to bet much money on that this is what
all compilers will do (and the OP is obviously using some em-
bedded system, so there's a non-vanishing chance that he's not
using gcc)...
Regards, Jens
PS: Finally again some nit-picking discussion on clc - I really
was missing them, always learned a lot from them;-) Thanks!