[comp.std.c] ansi printf questions

blarson@skat.usc.edu (Bob Larson) (02/04/89)

While writing a portable vsprintf implemtation (to be submitted to
comp.sources.misc) I noticed a couple of oddities in the K&R 2
description of printf formats:

How does %i differ from %d?  Is this just for compatability with
scanf strings?

u, o, x, and X formats take an int and print it unsigned.  Shouldn't
they take an unsigned int?  Is there a difference on any known machine?

-- 
Bob Larson	Arpa: Blarson@Ecla.Usc.Edu	blarson@skat.usc.edu
Uucp: {sdcrdcf,cit-vax}!oberon!skat!blarson
Prime mailing list:	info-prime-request%ais1@ecla.usc.edu
			oberon!ais1!info-prime-request

gwyn@smoke.BRL.MIL (Doug Gwyn ) (02/04/89)

In article <15159@oberon.USC.EDU> blarson@skat.usc.edu (Bob Larson) writes:
>How does %i differ from %d?

Is there a %i?  I don't have the proposed Standard at hand but I don't
recall a %i format.  I'll look this up and if nobody else responds by
the time I find out, I'll let you know what %i is supposed to do.

>u, o, x, and X formats take an int and print it unsigned.  Shouldn't
>they take an unsigned int?  Is there a difference on any known machine?

Unsigned int is (now) required to be passed in exactly the same format
as plain int, so it doesn't matter from the viewpoint of implementing
printf().  It does matter from the viewpoint of the USER of printf(),
and this has been fixed in the latest edition of the proposed Standard.

bill@twwells.uucp (T. William Wells) (02/05/89)

In article <9591@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
: In article <15159@oberon.USC.EDU> blarson@skat.usc.edu (Bob Larson) writes:
: >How does %i differ from %d?
:
: Is there a %i?  I don't have the proposed Standard at hand but I don't
: recall a %i format.  I'll look this up and if nobody else responds by
: the time I find out, I'll let you know what %i is supposed to do.

%i is the same as %d, according to the May draft.

(BTW, if anyone can tell me how to get the latest and greatest, I'd
appreciate e-mail.)

---
Bill
{ uunet!proxftl | novavax } !twwells!bill

gwyn@smoke.BRL.MIL (Doug Gwyn ) (02/05/89)

In article <372@twwells.uucp> bill@twwells.UUCP (T. William Wells) writes:
>%i is the same as %d, according to the May draft.

Yes, it's the same in the proposed standard submitted to X3.
Undoubtedly this was added simply for symmetry with the scanf %i.

blarson@skat.usc.edu (Bob Larson) (02/06/89)

Thanks to all who answered %i is the same as %d in *printf.  (They
do differ in *scanf.)

One further question:  On a machine with 16 bit short and 32 bit int,
what should:

printf("%hd", 65537);

print?  65537 (ignore the h)?  1 (truncate to short)?  unspecified?

-- 
Bob Larson	Arpa: Blarson@Ecla.Usc.Edu	blarson@skat.usc.edu
Uucp: {sdcrdcf,cit-vax}!oberon!skat!blarson
Prime mailing list:	info-prime-request%ais1@ecla.usc.edu
			oberon!ais1!info-prime-request

gwyn@smoke.BRL.MIL (Doug Gwyn ) (02/07/89)

In article <15188@oberon.USC.EDU> blarson@skat.usc.edu (Bob Larson) writes:
-One further question:  On a machine with 16 bit short and 32 bit int,
-what should:
-printf("%hd", 65537);
-print?  65537 (ignore the h)?  1 (truncate to short)?  unspecified?

Unspecified.  Do whatever you like.

tony@hp-sdd.hp.com (Tony Parkhurst_TEMP) (02/07/89)

In article <15188@oberon.USC.EDU> blarson@skat.usc.edu (Bob Larson) writes:
>Thanks to all who answered %i is the same as %d in *printf.  (They
>do differ in *scanf.)

>One further question:  On a machine with 16 bit short and 32 bit int,
>what should:

>printf("%hd", 65537);

>print?  65537 (ignore the h)?  1 (truncate to short)?  unspecified?


This is answered in the same way as your other question.  Only makes a
difference in scanf.  Remember that shorts are promoted to ints when
passed on the stack.  So, since your constant is not qualified, the
65537 is passed to printf as an int.  Since you can't pass a short on
a stack, 'h' is ignored.  In scanf, you pass a pointer to a short, so the
'h' is necessary.

If you are planning on posting your vprintf, then you might consider one more
difficulty, that is, ints being 16 bits and longs being 32.  Creates no
end of problems, but these are still used.  In this case, 'l' is very important
in both printf and scanf.

-- Tony

-- 

Tony Parkhurst	( tony@hp-sdd.HP.COM )

"Is this Hell?  Or is this Texas?" "Both" -- Heinlein, _J_O_B: _A _C_o_m_e_d_y _o_f _J_u_s_t_i_c_e