Is table test result not the same as compiled?

-2

The code below when executed returns the result for m = 0 , but doing the table test the result should be -2 .

I can not prove 0 as a result unless the variable t++ when in (t * t++) , only this moment and only it, would get the value 4, which is also strange. I tested it in PHP and C.

 int t = 3;
 int p = -5;
 int q = 1;
 int m = (q * p + (t * t++)- (t + q))-2;
    
asked by anonymous 05.10.2018 / 06:04

1 answer

2

Obviously your table test is wrong. Desktop tests need to simulate what the computer does and not what you want it to do. I do not even know if it is the case of table test, just do the simple mathematical calculation.

Maybe you're talking about the undefined behavior in C that is to change the value of t in progress in the expression . Take out ++ and give the result you expect. If you need it you need to see where you want to go and put the expression in another way. Never use an operator that causes a side effect on the variable in the middle of an expression.

In PHP it may be different, in another version of C compiler or another 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);
}

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

    
05.10.2018 / 06:26