From: jlm@ugcs.caltech.edu (Jacob Mandelson) Newsgroups: alt.humor.best-of-usenet Subject: [comp.lang.c] Re: Undefined behavior and logic. Date: 16 Aug 1997 10:32:03 GMT Subject: Re: Undefined behavior and logic. From: "Dann Corbit" Newsgroups: comp.lang.c Alicia Carla Longstreet wrote in article <33D7F61F.18D3@ici.net>... > I know that this is undefined behavior, since in most every case you are > changing the value of a data object two or more time between sequence > points. > Question, I can understand some of the answers, but there is one I do > not understand: > [see below] [snip] > n = 3; > n = n*(n--); > printf("3. n = %d\n", n); > This is the one I do not understand: > If the expression n*(n--) is evaluated left to right: > > 3*3 > 9 > > If the expression n*(n--) is evaluated right to left: > > 2*3 > 6 > > This is obviously undefined behavior, so I understand that whatever the > compiler produces is acceptable. I further understand that logic may > not apply. So here is my question: > > Two compilers produced 8 for this particular expression, is there any > logic to producing this particular value? Of course there is a logical explanation. Computers are just hardware switches turned rapidly on and off, after all. 0. The instruction sequence n*(n--) is attempted. 1. The computer is multi-threading and two attempts to modify the same storage location "n" are made simultaneously. 2. The CPU issues a non-maskable interrupt to invoke recovery procedures. 3. You are running Windows95 and a modal VB form has already attempted to mask all interrupts. 4. VB has re-routed the interrupt handler, but did not expect this message. 5. A stream of 16 bytes is written to the Kernel of the OS. 6. This same computer was controlling air traffic for an Ontario Canada airport. 7. A plane recieves a new vector, and flies directly into the tower. 8. It just so happens that Scott Nudds was on the plane. His body hits the back of your head at 545mph, and flies out of your nose. 9. The spatters of blood on the monitor for the number "8" Of course there are additional ways that "8" could come about. In fact, absolutely any number is plausible. [snip]