is printf() broken?

Stefan Farfeleder stefan at fafoe.narf.at
Sun Sep 5 12:56:54 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.

The output is correct:

e, E   A double argument representing a floating-point number is converted in the
       style [-]d.ddd e±dd, where there is one digit (which is nonzero if the
       argument is nonzero) before the decimal-point character and the number of
       digits after it is equal to the precision; if the precision is missing, it is taken as
       6; if the precision is zero and the # flag is not specified, no decimal-point
       character appears. The value is rounded to the appropriate number of digits.
[...]

Cheers,
Stefan


More information about the freebsd-standards mailing list