Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!rutgers!ucla-cs!ames!ucbcad!ucbvax!NGP.UTEXAS.EDU!mic
From: mic@NGP.UTEXAS.EDU (Mic Kaczmarczik)
Newsgroups: mod.computers.vax
Subject: Re: printf() bug in VMS C 2.0?
Message-ID: <8612090334.AA13495@ngp.utexas.edu>
Date: Mon, 8-Dec-86 22:34:50 EST
Article-I.D.: ngp.8612090334.AA13495
Posted: Mon Dec  8 22:34:50 1986
Date-Received: Sat, 13-Dec-86 22:04:44 EST
References: <8612041854.AA23322@ucbvax.Berkeley.EDU>
Sender: daemon@ucbvax.BERKELEY.EDU
Reply-To: ngp!mic@ngp.utexas.edu (Mic Kaczmarczik)
Organization: UTexas Computation Center, Austin, Texas
Lines: 62
Keywords: printf(), VAX C
Approved: info-vax@sri-kl.arpa
Summary: %02d does the trick, whether there's a bug or not.

In article <8612041854.AA23322@ucbvax.Berkeley.EDU> levy@ucbvax.Berkeley.EDU@ttrdc.UUCP writes:
>I have found what appears to be a bug in printf under VMS C 2.0 (running on
>VMS 4.3).
>
>main()
>{
>	
>	printf("X%2.2dX\n",2);
>}
>
>will produce the output line
>X 2X
> ^
> |
> +-[this is a blank]
>
>when compiled in the aforementioned VMS C.
>
>Under all versions of UNIX C compilers that I have tried, I get what I want:
>X02X
>
>Is this a "known bug"?  Is there some workaround which doesn't require using
>different format strings depending on the value of the number being printed?
>(I want to print numbers in the range 0-99 and have them come out as precisely
>two digits.  And I want to do it portably in C.)  THANKS....
>--
> -------------------------------    Disclaimer:  The views contained herein are
>|       dan levy | yvel nad      |  my own and are not at all those of my em-
>|         an engihacker @        |  ployer or the administrator of any computer
>| at&t computer systems division |  upon which I may hack.
>|        skokie, illinois        |
> --------------------------------   Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa,
>	   go for it!  			allegra,ulysses,vax135}!ttrdc!levy

Try

	printf("X%02dX\n",2);

This should do exactly what you said you wanted: print 2-digit
numbers, padded on the left with 0's.  (Of course, that's only as long
as the numbers are between 0 and 99.  If the number is 100, the number
will use three spaces anyway.)

Now, is the way VAX C interprets the format string "%2.2d" a bug?  If
it don't work like Unix, it's probably a bug :-)...  However, the
Berkeley 4.3 printf() man page indicates the first 2 is a "field
width", basically meaning the %d conversion should output *at least*
that many spaces.  The 2 after the period is the "precision", meaning
the *maximum* number of spaces the field should use.  Going by this, I
wouldn't have expected the output to be zero-padded (if I use a
precision on a string, do I want it zero-padded?), but perhaps it
makes sense in some situations to have %2.2d work that way.

Any C wizards care to comment?

Mic Kaczmarczik
User Services Digital Support Group
UT Austin Computation Center

...!ihnp4!seismo!ut-sally!ut-ngp!mic
mic@ngp.utexas.edu
ccep001@UTADNX.BITNET