"%02d" vs "%.2d"

Kenneth Almquist ka at june.cs.washington.edu
Tue Jan 31 07:58:08 AEST 1989


chris at mimsy.UUCP (Chris Torek) writes:
>
> 	printf("%0*d", width, value)
> 
> works fine.  (`Note that 0 is a flag, not a field width'....)
> 
> (Of course, there may be any number of implementations that got this wrong.)

Well, this is close to being right.  Zero probably *should* have been
defined to be a flag.  And as far as I know all UNIX implementations of
printf actually parse zero as though it were a flag (which is not a bug
since the behavior of printf is undefined for illegal format strings).

HOWEVER, the printf documentation for System V and 4.3 BSD both agree
that zero is not a flag:

	"...the conversion specification includes...an optional digit
	string specifying a *field* *width*; ... if the field width
	begins with a zero, zero-padding will be done....  A field
	width...may be `*' INSTEAD of a digit string."

This bit of confusion is another reason for using the precision field
when you want zero padding.
				Kenneth Almquist



More information about the Comp.lang.c mailing list