I've never heard of any rule specifying whether the left side of == is
evaluated before or after the right side. I'm pretty sure there is no
such rule. That means the sequence is at the compiler's convenience.
I would expect (but would never rely on) the compiler to place the
harder side first in most cases. That is what you are seeing. The
side of the == containing the ++ was evaluated first in each example.
Arthur Schwarz wrote:
For the Short Code Snippet we have the Following Results. I am using gcc-version 3.4.4 under Cygwin.
I would have expected the 2nd result to have been equal with the post-increment occurring after the comparison operation. Does the precedence of the closing ')' force the unary post-increment to execute before the comparison operation.
I would have expected that the last result would have been not equal. What seems to be happening is either the original 'num' is being compared to itself or the pre-increment 'num' is being compared to itself. At first blush, this appears wrong. Have I misunderstood the semantics?
skidmarks
/************** Short Code Snippet *************/
int main() {
int num = 10;
if ( num++ == num ) { printf("( num++ == num )\n"); }
else { printf("( num++ != num )\n"); }
if ( num == num++) { printf("( num == num++)\n"); }
else { printf("( num != num++)\n"); }
if (++num == num) { printf("(++num == num )\n"); }
else { printf("(++num != num )\n"); }
if ( num == ++num) { printf("( num == ++num)\n"); }
else { printf("( num != ++num)\n"); }
}
/************** Following Results *************/
( num++ != num ) OK
( num != num++) Not so OK
(++num == num ) OK
( num == ++num) Not So OK