printf() problem

Thomas Hameenaho thomas at uplog.se
Fri Apr 28 00:04:52 AEST 1989


In article <11657 at hodge.UUCP> jdm at hodge.UUCP (jdm) writes:
       Perhaps someone could explain this printf() phenomena to me.

       I have a file with binary data.  I want to read four consecutive
       bytes from the file and display them with printf().  The data
       in in the file in hex is:

	   92 AB 4E 33

       I fopen() the file in binary mode and used the following line
       of code to read and print out the data:

	   printf("%x %x %x %x\n", getc(fp), getc(fp), getc(fp), getc(fp));

       printf() displays it as:

	   33 4E AB 92

The problem is that the evaluation order of the arguments to printf (in
this case) is not defined in C. One should NOT have expressions with
sideeffects in function calls. The code will be inherently non-portable.
--
Real life:	Thomas Hameenaho		Email:	thomas at uplog.{se,uucp}
Snail mail:	TeleLOGIC Uppsala AB		Phone:	+46 18 189406
		Box 1218			Fax:	+46 18 132039
		S - 751 42 Uppsala, Sweden



More information about the Comp.lang.c mailing list