printf() problem
Geoff Rimmer
geoff at cs.warwick.ac.uk
Fri Apr 28 13:13:45 AEST 1989
In article <11657 at hodge.UUCP> jdm at 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 at 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 |
\---------------------------------------------------------------/
More information about the Comp.lang.c
mailing list