0

I was doing some exercises on the order of execution of operations in C and I came across a case that I did not quite understand.

int d = 1;
int e = d--/10;     // the result of e will be 0;

before calculating the value of "e", we decremented the "d" then we did the division.

on the other hand in "f", we made the division before decrementing the "d"!

int d = 1;
int f = 10/d--;     // the result of f will be: 10

My question is: why there is a differentiation in the value of "d" used knowing that in both cases the decrementation of "d" is a post-decrement?

Nimpo
  • 430
  • 6
  • 13
  • 4
    The value of `d--` is the value of `d` *before* the decrement. So `d--/10` is equal to `d/10`, and `d` is then reduced to `0`. But `1/10` and `0/10` are both `0`. – Barmar Mar 30 '22 at 23:55
  • This is the difference between pre-increment and post-increment. – Barmar Mar 30 '22 at 23:55
  • 2
    Post-decrement is used in both scenarios. This isn't about the difference between post-decrement and pre-decrement. Reopened. – ikegami Mar 31 '22 at 02:48
  • 2
    Very close to a dupe of [What is the difference between ++i and i++?](https://stackoverflow.com/q/24853/3422102) under the Linked entries to the right. – David C. Rankin Mar 31 '22 at 05:43
  • 1
    This question is actually about difference between `1/10` and `10/1`. Not related to any post- vs. pre-increment questions at all. – Gerhardh Mar 31 '22 at 07:08
  • 2
    Attempting to read the mind of whoever wrote the exercise, they might have tried to hint that `10/d--` will work but `10/--d` will crash and burn due to division with zero. The true lesson learnt is that mixing the `++` and `--` operators with other operands in the same expression is very bad practice. If you never do, then you don't have to worry about post increment vs pre increment either - everyone wins. – Lundin Mar 31 '22 at 09:38

2 Answers2

5

There's actually no difference. It uses d=1 and does a post-decrement in both cases.

The reason you see an apparent difference is that you're doing integer division, which rounds towards 0. That is: (int)1 / (int)10 = 0.

See the accepted answer on What is the behavior of integer division?

Scott Leis
  • 2,810
  • 6
  • 28
  • 42
1

For

int e = d--/10;

You say

before calculating the value of "e", we decremented the "d" then we did the division.

And this is the main source of your confusion. value of d was decremented after using it in division. It doesn't matter if it was in the expression before division, it is still post-decrement, and will happen after using the original value.

You are also doing interger division, which rounds towards zero, which may add to your confusion.

And in anticipation of possible follow-up question/experiment: if you have several post- or pre-increment or decrement operators in same expression for same variable, what actually happens is undefined. So just don't do that, results may change depending on compiler and optimization and whatnot.

hyde
  • 60,639
  • 21
  • 115
  • 176