is printf() broken?

Erik Trulsson ertr1013 at student.uu.se
Sun Sep 5 12:51:56 PDT 2004


On Sun, Sep 05, 2004 at 12:36:40PM -0700, Steve Kargl wrote:
> The following program
> 
> #include <stdio.h>
> int main(void) {
> 	int d;
> 	double x;
> 	x = 1.234E05;
> 	for (d = 0; d < 5; d++)
>     	printf("%+-31.*e\n", d, x);
> 	return 0;
> }
> 
> generates
> 
> +1e+05                         
> +1.2e+05                       
> +1.23e+05                      
> +1.234e+05                     
> +1.2340e+05                    
> 
> The question is whether the first number should be
> "+1.e+05".  That is, is the printing of the decimal
> point required or optional?  I only have Harbison
> and Steele's book and it does not state what the 
> expected behavior should be.

I believe printf() is correct, and that the printing of the decimal
point for the first number above is neither required nor optional but
actually not allowed at all

>From Harbison & Steele 5th edition:

 The e and E conversions 
 [...]
 If the precision is 0, then no digits appear after the decimal point.
 Moreover the decimal point also does not appear unless the # flag is
 present.

(Nearly identical text is also in H&S 3rd edition.)


>From n869.txt  (the last publically available draft of C99)

[the e and E conversions for fprintf]
[...]
 if the precision is zero and the # flag is not
 specified, no decimal-point character appears



-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013 at student.uu.se


More information about the freebsd-standards mailing list