Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!mcvax!ukc!warwick!geoff From: geoff@cs.warwick.ac.uk (Geoff Rimmer) Newsgroups: comp.lang.c Subject: Re: printf() problem Message-ID: <1783@ubu.warwick.UUCP> Date: 28 Apr 89 03:13:45 GMT References: <11657@hodge.UUCP> Sender: news@warwick.UUCP Organization: Computer Science, Warwick University, UK Lines: 50 In-reply-to: jdm@hodge.UUCP's message of 26 Apr 89 00:25:48 GMT In article <11657@hodge.UUCP> jdm@hodge.UUCP (jdm) writes: > printf("%x %x %x %x\n", getc(fp), getc(fp), getc(fp), getc(fp)); > > Although the order of the data in the file is: > > 92 AB 4E 33 > > printf() displays it as: > > 33 4E AB 92 > > Just the reverse of its order in the file. The 5 arguments to printf() are being pushed onto the stack, starting from the 5th back thru the 1st (the format string). There is nothing odd with this. When a C compiler is written, the author can evaluate arguments to functions in anyway he/she likes. Last to first, first to last, or even the even arguments followed by the odd ones! So, you should never have the code: main() { int i=1; printf("%d %d %d %d\n",i++,i++,i++,i++); } since some compilers will print 1 2 3 4 others will print 4 3 2 1 an a weird one might print 3 2 1 4 So, you cannot and must not rely on any particular behaviour. What works for your compiler may not work with another. Geoff /---------------------------------------------------------------\ | GEOFF RIMMER - Friend of fax booths, ANSI C, PCBH, | | phone *numbers* & MPFC & printf | | email : geoff@uk.ac.warwick.emerald | | address : Computer Science Dept, Warwick University, | | Coventry, England. | | PHONE : +44 203 692320 (10 lines) If I'm out please | | leave a message with my secretary. | | FAX : +44 865 726753 | \---------------------------------------------------------------/