Obviously your table test is wrong. Table tests need to simulate what the computer does and not what you want it to do. I don’t even know if it’s the table test case, just do the simple math.
Maybe you’re talking about undefined behavior in C which is to exchange the value of t
in progress in the expression. Take the ++
and will give the result you expect. If you need it you need to see where you want to go and assemble the expression in another way. Never use an operator that causes side effect on the variable in the middle of an expression.
In PHP may be different, in other compiler version C or other platform may be different.
#include <stdio.h>
int main(void) {
int t = 3;
int p = -5;
int q = 1;
int m = (q * p + (t * t) - (t + q)) - 2;
int n = (q * p + (t * ++t) - (t + q)) - 2;
t = 3;
int o = (q * p + (t * t++) - (t + q)) - 2;
printf("%d, %d, %d", m, n, o);
}
Behold working in the ideone. And not working in the repl it.. Also put on the Github for future reference.
You will "have fun" even more when you start debugging and realize that in
($t * $t++)
the "four" that Voce said is the... on the left..! and the three on the right :D (for that very reason, in PHP it is usually more interesting to use++$i
than$i++
in a loopfor
, where possible, to avoid creating an unnecessary reference)– Bacco
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero
I didn’t try to debug Bacco, but I will.
– Laiton Garcia