printf() problem

John Durko jdurko at iemisi.UUCP
Sat Apr 29 05:59:33 AEST 1989


In article <11657 at hodge.UUCP>, jdm at 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 <stdio.h>

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
      



More information about the Comp.lang.c mailing list