-2

I am trying to evaluate this, and even if is is quite simple, I can't seem to understand it. I got 16, but the provided answer was 12. I don't understand how this can be 12.

I did --x first, so first y would be 4, then I need to multiply with x--, but it will be 4 too since it is evaluated after, and the x being decremented after that to 3. So 4*4 = 16.

Can someone explain what is wrong in my reasoning?

int x, y;
x = 5;
y = --x * x--;
std::cout << y;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Clang says 16. GCC says 12. So we can conclude that this would be undefined behavior. In other words, don't write code like this. – General Grievance Aug 08 '23 at 18:14
  • 2
    You have UB.... – Jarod42 Aug 08 '23 at 18:14
  • 2
    in `a*b`, evaluating `a` in unsequenced with `b` evaluation. and you modify x twice. – Jarod42 Aug 08 '23 at 18:15
  • I don't know if that is a good idea to suggest C answer as duplicate for C++ question... (https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior) so probably should be another duplciate... but to my knowledge that behavior did not become defined in any version of C++ I know... – Alexei Levenkov Aug 08 '23 at 18:17
  • 3
    I am not sure why soo many people get these crazy problems. Your main takeaway should be to never write code like this. – drescherjm Aug 08 '23 at 18:18
  • Thanks everyone but I thought that in C++, expressions are evaluated from left to right so it would the first one then the second one so answer = 16. Maybe I'm confusing order of precedence and order of the compilation? – bestgamer14 Aug 08 '23 at 18:18
  • (Note I'm just learning C++, saw some questions like that in some MCQ's) – bestgamer14 Aug 08 '23 at 18:19
  • 1
    @bestgamer14 see [Operator Precedence](https://en.cppreference.com/w/cpp/language/operator_precedence) and [Order of evaluation](https://en.cppreference.com/w/cpp/language/eval_order). "*I did `--x` first, so first `y` would be 4*" - this is not true, since `y` is not assigned until after the result of the `*` operator is computed. Both `--x` and `x--` are being evaluated before that time, and both are modifying `x`, and the order in which they are evaluated in relation to each other is not guaranteed. – Remy Lebeau Aug 08 '23 at 18:20
  • @RemyLebeau I understand it better now, thanks for the help! – bestgamer14 Aug 08 '23 at 18:25
  • 3
    Just write the code so that the ordering is well-defined. `--x; y = x * x; x--;` Boom, done. – Eljay Aug 08 '23 at 18:28
  • 2
    @bestgamer14 Even worse, when I look at it, I can see how it might return 12 or **15**, not 16! If `--x` is evaluated first, it would set `x` to 4 and return 4, and then `x--` would set `x` to 3 and return 4, so `4*4=16`. But, if `x--` is evaluated first, it would set `x` to 4 and return 5, and then `--x` would set `x` to 3 and return 3, so `5*3=15`! So, all the more reason that you can't rely on the behavior of the original code. – Remy Lebeau Aug 08 '23 at 18:30
  • 1
    @bestgamer14 *saw some questions like that in some MCQ* -- I don't know what the fascination is in wasting time (and if it is a paid course, money) giving problems like this. It takes just a minute or two to explain that the behavior is undefined, and leave it at that. When I first learned `C`, this is how I learned to not write code like that -- just one time being told it is UB and to forget it -- there were no hours or even days being wasted on "figuring out" what the code is doing. – PaulMcKenzie Aug 08 '23 at 19:20

1 Answers1

5

You are not guaranteed whether --x or x-- is evaluated first, so the result is undefined.

user1806566
  • 1,078
  • 6
  • 18
  • it is worst than unspecified evaluation order, it is UB, so crash, no display are possible output; not just wrong value. – Jarod42 Aug 10 '23 at 22:27