I declared a variable suppose i = 1
And then I used unary decrement operator in printf function
printf("%d %d",i--,i);
I expected the output to be 1 0
but the output displayed was 1 1
Why the value of i
is not getting decremented?
I declared a variable suppose i = 1
And then I used unary decrement operator in printf function
printf("%d %d",i--,i);
I expected the output to be 1 0
but the output displayed was 1 1
Why the value of i
is not getting decremented?
The order of evaluation of function parameters is not guaranteed in C. It might be left to right, or it might be right to left. It's up to the compiler implementation.
It is undefined behaviour to have multiple references to a variable in combination with increment or decrement operators in one expression.
i--
decrements the value after being evaluated.
post-increment and post-decrement creates a copy of the object, increments or decrements the value of the object and returns the copy from before the increment or decrement. http://en.cppreference.com/w/cpp/language/operator_incdec
Furthermore, the order of evalation is not guaranteed. So i
(the second argument) could be evaluated before i--
(the first one)