On Sun, 15 Oct 2006 00:38:08 +0200, Leopold Toetsch wrote > Folks, > > Either perl 5 or libc is wrong (or my testcase is bogus, or the standard ...): > > $ perl -e'printf("!%03.2d!\n", 1)' > !001! > > $ cc -Wall sprintf.c -o sprintf && ./sprintf '%03.2d' 1 # [1] > ! 01! > > Parrot is currently following the 'official' aka libc behavior and is > returning the latter result [2].
I think the standard specifies the 0 flag in %d should be ignored when a precision is given. (However what I referred is JIS X 3010, that is the Japanese translation from ISO/IEC 9899, but not ANSI). I found it online, cf. http://man.he.net/man3/sprintf 0 specifying zero padding. (..snip..) If a precision is given with a numeric conversion (d, i, o, u, i, x, and X), the 0 flag is ignored. I think then printf("!%03.2d!\n", 1) should put out the same string as one from printf("!%3.2d!\n", 1), that is ! 01! P.S. Another suspicious behavior of perl5's (s?)printf is the following case: when a precision is negative (given through *). printf("!%3.*s!\n", -1, "ab"); # should be like printf("!%3s!\n", "ab"); printf("!%3.*s!\n", 0, "ab"); printf("!%3.*s!\n", 1, "ab"); I think they should print ! ab! ! ! ! a! but perl5 does ! ! ! ! ! a! Regards, SADAHIRO Tomoyuki