[comp.lang.c] "%02d" vs "%.2d"

chris@mimsy.UUCP (Chris Torek) (01/29/89)

In article <13368@ncoast.ORG> allbery@ncoast.ORG (Brandon S. Allbery) writes:
>One other reason as well:  it's not possible to get zero padding when using
>variable width fields (%*d) under the old scheme.

	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.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

ka@june.cs.washington.edu (Kenneth Almquist) (01/31/89)

chris@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