Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!uw-june!ka From: ka@june.cs.washington.edu (Kenneth Almquist) Newsgroups: comp.lang.c Subject: Re: "%02d" vs "%.2d" Summary: Zero is not a flag in printf. Message-ID: <7105@june.cs.washington.edu> Date: 30 Jan 89 21:58:08 GMT References: <104@rpi.edu> <198@broadway.UUCP> <6951@june.cs.washington.edu> <15690@mimsy.UUCP> Organization: U of Washington, Computer Science, Seattle Lines: 24 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