[mod.computers.vax] printf

levy@ucbvax.Berkeley.EDU@ttrdc.UUCP (12/04/86)

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

carl@CITHEX.CALTECH.EDU.UUCP (12/07/86)

Use
	printf("X%02.2dX\n", 2);
instead of
	printf("X%2.2dX\n", 2);

bzs@BU-CS.BU.EDU.UUCP (12/07/86)

From: levy@ucbvax.Berkeley.EDU@ttrdc.UUCP
>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

Use:

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

(this works on UNIX also and is "more correct", I'm not sure that what
you were relying upon didn't work by accident, see the printf man page.)

		-Barry Shein, Boston University

campbell@maynard.UUCP.UUCP (12/07/86)

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).
>...
>	printf("X%2.2dX\n",2);
>
>will produce the output line
>X 2X
> ...
>Under all versions of UNIX C compilers that I have tried, I get what I want:
>X02X
>
>Is this a "known bug"?

Yes, it is a "known bug" in your program.  You want:

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

I tried your example under VENIX (UNIX V7) and got "X 2X", as I should.
See K&R, page 146.  In fact, if the UNIX C compilers you tried really
produced "X02X", they disagree with K&R, and should be fixed.
-- 
Larry Campbell				     The Boston Software Works, Inc.
Internet: campbell@maynard.bsw.com	    120 Fulton Street, Boston MA 02109
uucp: {alliant,wjh12}!maynard!campbell  	    +1 617 367 6846
ARPA: campbell%maynard.uucp@harvisr.harvard.edu      MCI: LCAMPBELL

mic@NGP.UTEXAS.EDU (Mic Kaczmarczik) (12/09/86)

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

LEICHTER-JERRY@YALE.ARPA.UUCP (12/10/86)

Reply-To: <LEICHTER-JERRY@YALE.ARPA>

    ... [U]nder 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]
    
    ...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.)
Assuming you are reporting accurately the format string you are using, and
the output you are getting, you have indeed discovered a bug - in "all ver-
sions of UNIX C compilers that [you] have tried"!  The documentation of printf
is quite clear:  Fields are right-justified with SPACES, not 0's.  You can
request zero fill by preceeding the field width with a 0, as in:

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

							-- Jerry
-------