Search code examples
operatorsoperator-precedence

Precedence of operators in C


I know that suffix and postfix increment (decrement) have higher predence vs comparison (==) in C.

But I'm running into confusion right now, if I have 2 conditional loops like while (0 != i--) and while (0!= --i) , so what is the difference there? Because due to precedence the decrement should always be executed first then do the comparision?


Solution

  • i-- "uses" the value of i and then decrements it.

    --i decrements i and then "uses" the value of i.

    So if i=4 then while(0 != i--){ printf("%d\n", i); } would show 3 (because i is now decremented), 2, 1, 0 (because i was 1 when the check was done).

    In the while(0 != --i) { printf("%d\n", i); } you'd get 3 (i is still decremented), 2, 1 but not 0.i` was 0 when the check was performed because it was PREdecremented.