Re: unexpected result
- From: jaysome <jaysome@xxxxxxxxxxx>
- Date: Sat, 11 Mar 2006 01:43:05 -0800
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
.
- Follow-Ups:
- Re: unexpected result
- From: Richard Bos
- Re: unexpected result
- From: Richard Heathfield
- Re: unexpected result
- From: Keith Thompson
- Re: unexpected result
- From: Jordan Abel
- Re: unexpected result
- From: Mark F. Haigh
- Re: unexpected result
- References:
- unexpected result
- From: ashu
- Re: unexpected result
- From: Ankur
- Re: unexpected result
- From: Jack Klein
- Re: unexpected result
- From: jaysome
- Re: unexpected result
- From: Keith Thompson
- unexpected result
- Prev by Date: Re: Question on Pointers
- Next by Date: Re: memory use global vs auto
- Previous by thread: Re: unexpected result
- Next by thread: Re: unexpected result
- Index(es):
Relevant Pages
|