Re: increasing var twice in a statement

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

 



On 15/06/07, Willi Mann <willi@xxxxxx> wrote:
int main(void) {
        int a;
        int b;
        b = 3;
        a = ++b + ++b;
        printf("%d\n",a);
        return 0;
}

I would have expected 9, but gcc 4.1.2 produced a binary that wrote 10.

Getting an 8 would also be perfectly reasonable, based on the
side-effect definition of pre-increment.

If it were not undefined,
a = ++b + ++b;
would be equivalent to
a = (b+1) + (b+1);
with 2 side-effects of
b = b+1;
occuring some point before the sequence point and after the evaluation
of the respective b.

But because of those side effects, the behaviour is undefined, as has
been mentioned.  Maybe you'll get lucky and the compiler will start
Nethack for you. ;)

~ Scott

[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