C++ Bug or (my) Interpretation error

Arthur Schwarz aschwarz1309@verizon.net
Sun Sep 14 19:24:00 GMT 2008


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



More information about the Gcc-help mailing list