Re: unexpected result



Keith Thompson wrote:
jaysome <jaysome@xxxxxxxxxxx> writes:

Jack Klein wrote:

[...]

ashu wrote:

[...]

include <stdio.h>
int main(void)
{
int i,j=2;
i=j++ * ++j * j++;
printf("%d %d",i,j);
return 0;
}

[...]

No, it is not "all compiler dependent", it is undefined behavior.
There is no right result, there is no wrong result, as far as the C
language is concerned there is no need for a result at all.

There's always a result. But the result may not be as expected. Hence
unexpected behavior.


No, there is not always a result. One of the infinitely many possible
consequences of undefined behavior is that the code might not compile
in the first place. Other possibilities are that the program could do
nothing at all, or format your hard drive, or melt your monitor, or
make demons fly out of your nose.

I'd believe you if you could give me an example of a compiler that does nothing at all, or formats my hard drive, or melts my monitor, or makes demons fly out of my nose, when presented with the following code:

int main(void)
{
int i = 0;
i = i++;
return 0;
}

The fact is, you can't. And even if you could, then I bet many in here would agree that such a compiler is "broken", however twisted that may sound.

While many in here might understand what you're trying to get at with your examples, some may not. The following dialogue may help to illustrate my point:

<dialogue>
Son: "Dad, I want to learn how to program in C."

Dad: "Son, although I admire your goal, I've been reading a lot in CLC lately, and based on what they're saying in there, I'm pretty shocked about how a simple, honest mistake you might make could lead to formatting my hard drive. So I have to say no on this one."

Son: "Oh, come on Dad!"

Dad: "Sorry son. C programming is just too dangerous."

Son: "Okay Dad. But can I at least surf the web tonight after you go to bed?"

Dad: "Sure boy, that can't be nearly as bad as making a mistake in C."
</dialogue>

I've seen code like the following:

i = i++ % ARRAY_SIZE; /* increment circular buffer index */

and never thought twice about it--until I *really* started reading this newsgroup. Perhaps others will have a similar experience. I sure hope so.

Best regards
--
jay






.



Relevant Pages

  • Re: unexpected result
    ... int main ... nothing at all, or format your hard drive, or melt your monitor, or ... Dad: "Son, although I admire your goal, I've been reading a lot in CLC ...
    (comp.lang.c)
  • Re: Repost - Breaking In/gg
    ... Because your poem?s narrator is focused on the boy, ... both seem to set the tone of the relationship between father ... Though I suppose a "ritual Dad" would help his son with this ...
    (rec.arts.poems)
  • Liverpool joke
    ... SON "Dad, my mates in school told me that Liverpool won the ... DAD "Yes son, it's true, but they were dead lucky son, all the way ... SON "Jesus Christ - so Liverpool beat the English Champions elect too" ... against the best defence in Europe" ...
    (rec.sport.soccer)
  • Re: Liverpool joke
    ... SON "Dad, my mates in school told me that Liverpool won the ... DAD "Yes son, it's true, but they were dead lucky son, all the way ... SON "Jesus Christ - so Liverpool beat the English Champions elect too" ... against the best defence in Europe" ...
    (rec.sport.soccer)
  • Raising boys that feminists will hate: Part Five
    ... I suggest rebellion-unless you're cool with your son acting like JLo. ... Mom and dad, ... give the female chauvinist pigs any ground by pitting one form of masculinity ... Finally, parent, can you imagine the angst when Hollywood and the multitudinous, ...
    (soc.men)