This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ Bug or (my) Interpretation error


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]