Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!lll-winken!uunet!jtsv16!isdserv!iemisi!jdurko From: jdurko@iemisi.UUCP (John Durko) Newsgroups: comp.lang.c Subject: Re: printf() problem Summary: order args are pushed Keywords: C printf Message-ID: <481@iemisi.UUCP> Date: 28 Apr 89 19:59:33 GMT References: <11657@hodge.UUCP> Organization: Boeing Canada, Toronto, Canada Lines: 47 In article <11657@hodge.UUCP>, jdm@hodge.UUCP (jdm) writes: > Perhaps someone could explain this printf() phenomena to me. > > 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 The problem here ( I think :-} ) is that the compiler is pushing the function arguments on the stack from right to left. Therefore the last argument on the list is the first to be evaluated and the thus makes the first call to getc(). The following code illustrates this: #include main(){ int i = 1; printf(" %d %d %d %d \n",i++,i++,i++,i++); } Compiled and run this produces 4 3 2 1 . Can anybody tell me if this behavoir is "guaranteed" in C. I know that each argument expression must be evaluated fully before the next but could find no "guarantee" on the order that it is done. -- ______________________________________________________________________________ ___ ___ ___ ___ _ ___ / / / / / / /| / / John Durko, Boeing Canada, Toronto. /-- / / /-- / / | / / - The opinions expressed herein are not /__/ /__/ /__ _/_ / / /__/ official until the urinalysis results de Havilland Division are in!!! UUCP: {geac|utzoo|utgpu}!syntron!jtsv16!marsal1!iemisi!jdurko {uunet|suncan}!jtsv16!marsal1!iemisi!jdurko