Problem with printf()

Greyham Stoney greyham at ausonics.OZ
Thu Oct 13 14:46:37 AEST 1988


in article <504 at imec.UUCP>, croes at imec.uucp (Kris Croes) says:
>   main()
>   {
>   int i = 17;
>   float f = 17.0;
> 
>   printf("%d %f\n",i,i); /*1*/
>   printf("%d %f\n",f,f); /*2*/
            ^^       ^
>   }
You have indeed broken the law. These sort of things just aren't defined;
the ENTIRE output of BOTH printfs will be garbage (machine/compiler/system-
dependant == garbage). float and ints probably (do usually) have different
sizes, so a different ammount of info is passed for each one. When printf skips
the space for the first 'f' in the arg list, it is actually skipping
sizeof(int), cos that's what you told it was there; so it lands in the middle
of the float when it looks for the second argument.

> caused by printing the float under the "%d" format. I know that
> normal people don't do such a thing, but that is no reason for printf()
> to mess up the stack. (???)

Yes it is.... you lied when you told it what the stack looked like.
-- 
# Greyham Stoney:      (disclaimer not necessary: I'm obviously irresponsible)
# greyham at ausonics.oz - Ausonics Pty Ltd, Lane Cove. (* Official Sponsor *)



More information about the Comp.unix.wizards mailing list