C++ Bug or (my) Interpretation error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux