You would come to see that Increment/Decrement operators have a higher priority than * and consequently, value is first incremented and then multiplied. Thus, leading to a different result as expected.
You could split it to preserve your expected result
int produce_seq()
{
static int value = 0;
int res = value*value;
value ;
return res;
}
Again, it'd be great if someone could validate my answer as I think this i right cuz we used to get output based questions in school on the same concept, however the answers before seem to suggest something slightly different. So, guys do validate it , thanks!
CodePudding user response:
intially Value = 0;.
Value means = 1;
(value*value )
0 * 1 = 0
1 * 2 = 2
2 * 3 = 6
3 * 4 = 12
try to change (value * value)