printf bug: printf("%.f",0.1) == 2 !?

J. Johnston jjohnstn@redhat.com
Thu Jul 31 14:51:00 GMT 2003


Thanks for catching this.  The patch is fine.  I have committed it with
minor white-space changes.

-- Jeff J.

Honda Hiroki wrote:
> Hi,
> 
> I ported newlib-1.11.0 to an inhouse embedded system (with an original
> CPU) and noticed that
> 
>   #include <stdio.h>
>   char buf[16];
>   int main(void) {
>     int  n = sprintf(buf, "%.f", 0.1);
>     printf("[%s] %d\n", buf, n);       /* should print "[0] 1" */
>     return 0;
>   }
> 
> prints "[0] 2".
> 
> The attached one-line patch fixed the problem,
> but I am not sure whether or not the patch has evil side effects.
> 
> Is there any moderator who will investigate this problem?
> 
> Regards,
> 
> ====
> HONDA Hiroki
> R&D Center, IP Flex Inc.
> http://www.ipflex.com/english/index.html
> 
> ================================================================
> --- newlib-1.11.0/newlib/libc/stdio/vfprintf.c.orig  2 Jul 2003 02:27:30 -0000
> +++ newlib-1.11.0/newlib/libc/stdio/vfprintf.c  29 Jul 2003 10:04:04 -0000
> @@ -830,7 +830,7 @@
>                                         if (prec || flags & ALT)
>                                                 size += prec + 1;
>                                 } else  /* "0.X" */
> -                                       size = prec + 2;
> +                                       size = (prec || flags & ALT) ? 2 + prec : 1;
>                         } else if (expt >= ndig) {      /* fixed g fmt */
>                                 size = expt;
>                                 if (flags & ALT)
> 




More information about the Newlib mailing list