Xref: utzoo comp.lang.c:35230 comp.os.msdos.programmer:2820 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!apple!usc!wuarchive!uunet!mcsun!ukc!harrier.ukc.ac.uk!zodiac.ukc.ac.uk!cur022 From: cur022%cluster@ukc.ac.uk (Bob Eager) Newsgroups: comp.lang.c,comp.os.msdos.programmer Subject: Re: Possible C compiler bug on 8086 machines Message-ID: <21571.2792bbb4@cluster@ukc.ac.uk> Date: 15 Jan 91 08:22:12 GMT References: <3577@bruce.cs.monash.OZ.AU> Organization: Computing Lab, University of Kent at Canterbury, UK. Lines: 42 In article <3577@bruce.cs.monash.OZ.AU>, alanf@bruce.cs.monash.OZ.AU (Alan Grant Finlay) writes: > The following program demonstrates a printf that doesn't seem to work > properly when compiled using a variety of C compilers for an 8086 machine. > The program is as follows: > -----------------------------//--------------------------- > > #include > > main() > { > int x,y; > x = 65536/512; > y = 512; > printf("This works : %d, %d\n",x,y); > printf("This doesn't work : %d, %d\n",65536/512,512); > } > > ----------------------------//------------------------------ > > And here is a sample output: > > This works : 128, 512 > This doesn't work : 128, 0 > The compiler sees the 65536 in the second printf, and says: "This can't be represented in an int, I need a long". I wonder if the 'rules' say that it has to do this; certainly for an expression involving variables, the presence of a long in an expression coerces the whole expression to long. Given the above, it is easy to see what is happening. A four byte item is pushed on the stack when printf is called: this represents 6655536/512 as a long. The lower two bytes (the first ones on an 80x86) will contain the 128, and are processed correctly. The second two bytes will be zero, and are printed as the second value. I bet if you change the first %d to %ld in the second printf, the expected answer will appear! -------------------------+------------------------------------------------- Bob Eager | University of Kent at Canterbury | +44 227 764000 ext 7589 -------------------------+-------------------------------------------------