0

In cases like:

int q = 3;
++q * ++q

It will be an undefined behaviour in C.

However, what about the following scenarios?

++q * q
++q * q++

Note: I am not asking what is the definition of undefined behaviour.

My question is: What are the specific rules to help us to determine whether an expression will be an undefined behaviour especially when pre-increment is involved?


I found this piece of information online:

The behavior of modifying the value of an object through the evaluation of an expression more than once between sequence points is undefined. The behavior of using the value of an object in one expression when its being modified in another expression without an intervening sequence point is also undefined.

Does it mean that if the same variable in a single expression is changed more than once, it will be an undefined behaviour?

user3437460
  • 17,253
  • 15
  • 58
  • 106

1 Answers1

0
  • ++q * q

    Let's say q is 4. The compiler is free to evaluate the operands in any order it pleases, so you can have 5 * 5 = 25 (if the compiler evaluates first the left operand) or 5 * 4 = 20 (if the compiler evaluates first the right operand).

  • ++q * q++

    Let's say q is 4. The compiler is free to evaluate the operands in any order it pleases, so you can have 5 * 5 = 25 (if the compiler evaluates first the left operand) or 6 * 4 = 24 (if the compiler evaluates first the right operand). Explanation:

    1. Evaluate ++q; result is 5, q is now 5. Then evaluate q++; result is 5, q is now 6. Evaluate 5 * 5.

    2. Evaluate q++; result is 4, q is now 5. Evaluate ++q; result is 6, q is now 6. Evaluate 6 * 4.

AlexP
  • 4,370
  • 15
  • 15
  • So are these 2 cases undefined behaviour as well? – user3437460 Nov 29 '17 at 15:44
  • Since the results depend on the state of mind of the compiler at the moment when it compiles the expressions, yes, they are undefined behavior. *"The behavior of using the value of an object in one expression when its being modified in another expression without an intervening sequence point is also undefined.*" – AlexP Nov 29 '17 at 15:45
  • So shall I say this rule `The behavior of modifying the value of an object through the evaluation of an expression more than once between sequence points is undefined` corresponds to `++q * ++q` and this rule `The behavior of using the value of an object in one expression when its being modified in another expression without an intervening sequence point is also undefined.` corresponds to `++q * q` ? Are there any other rules other than the 2 I stated in my question? – user3437460 Nov 29 '17 at 15:48
  • See "[Does `a[i] = i;` always result in well defined behaviour](https://stackoverflow.com/questions/9556187/does-ai-i-always-result-in-well-defined-behaviour?rq=1)" for a spirited discussion. Basically, always remember that C does not guarantee the order in which operands or function arguments are evaluated, and don't make your code depend on it. And always make sure that all the variables you use in computations have a defined value. – AlexP Nov 29 '17 at 15:52