bug? in turbo c++
Roger Gonzalez
rg at msel.unh.edu
Mon Mar 11 23:40:46 AEST 1991
My stupid trivial program that started this whole sordid thread, which has
involved countless flames against me and my ancestry:
>>>main()
>>>{
>>> int i;
>>> long j;
>>>
>>> for (i = 0; i < 1000; i++, j = i*2)
>>> printf("oh crud: %x %10d %x\r", i, j, i);
>>>} ^
I was wondering why I had to use %ld
instead of %d, since I never had done so
in the past. I wondered if it was an
ansi-ism, and if K&R cc's were just
smarter about %d and could deduce size.
The correct answer as a few people pointed out, is that printf needs to
know how many bytes to slurp off the stack. I had had the luck to
always have used machines with sizeof(int)==sizeof(long). Also, I've
always used machines with "correct" :-) byte ordering. Since I was
using a PC, a machine I generally try to avoid, I forgot about Intel
byte ordering. (Little-endian? Never actually had cause to use that
term) So, the high bytes of 'j' get read in as the value for the last
'i' printed. Since I never went above 1000, this was always 0. On a
big-endian machine with sizeof(int) != sizeof(long), the output should be
(for example)
a 0 20
on a PC it is
a 20 0
Voila. I've been enlightened.
>ch at dce.ie (Charles Bryant) correctly but pointlessly writes:
>
>>Surely I am not the only person to notice that j is used before being
>>set? (On the first pass only). Maybe our newsfeed is just clogged.
True. However, since this was supposed to be just a minimalist program to
demonstrate concept, and when I was reducing the screen real estate it
took to list the code, I moved a 'j = i * 2' into the 'for' without a 'j = 0'.
My booboo.
In article <1906 at pdxgate.UUCP> mwizard at eecs.cs.pdx.edu (Craig Nelson) writes:
>No, you're not the only one. I wonder if he tried this instead, and then
>check for his 'bug':
>
>main()
>{ int i=0;j=0;
>
> for (;i<1000;i++,j=i*2)
> printf("oh crud: %x %10d %x\r", i, j, i);
>
>}
>
>Also, I was always under the impression that long defaulted to an
>integer format (a long one). I wonder why he is trying to format it
>to a double format in the printf() statement ?
>
>Craig Nelson (mwizard at eecs.ee.pdx.edu)
Huh? I think you're not only missing the point, but just plain -wrong-
to boot.
AT ANY RATE...
Thank you, if you were polite and helpful..
Eat cow dung and die slowly, if you flamed me..
You wouldn't believe the number of postings that said, in effect, "hey
stupid - you should use %ld, not %d!" I knew that. Reread the posting;
I wrote "it works correctly with %10ld." I asked WHY was this compiler
MAKING me use %ld, when I had gotten away with %d in the past. There's
a difference.
What are the morals of this story?
- using the same types of machines all your life can be hazardous to
your programming style
- if you want wide readership, post about possible bugs in popular programs
- the less people understand the WHY's, the more emphatically they expound
on their learned-by-rote rules, as if it were a religious chant
- there are just as many assholes on this group as any other group
Cheers,
Roger
--
"The question of whether a computer can think is no more interesting
than the question of whether a submarine can swim" - Edsgar W. Dijkstra
rg@[msel|unhd].unh.edu | UNH Marine Systems Engineering Laboratory
r_gonzalez at unhh.bitnet | Durham, NH 03824-3525
More information about the Comp.lang.c
mailing list