This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/2194] New: printf: meaning of precision for 'd', 'i', 'o', 'u', 'x', 'X' directives


Hi, 
 
In a printf(3) format directive such as "%.10d", what does the precision 10 
mean? ISO C 99 section 7.19.6.1. paragraphs 4 and 8, and POSIX 
(http://www.opengroup.org/susv3/functions/fprintf.html) both say: 
 
  "The precision specifies the minimum number of digits to appear; 
   if the value being converted can be represented in fewer digits, 
   it is expanded with leading zeros." 
 
Here is a test program: 
 
========================================= 
#include <locale.h> 
#include <stdio.h> 
int main () 
{ 
  setlocale(LC_ALL,""); 
  printf("%#'.10d\n", 1); 
  printf("%#'.10d\n", 12); 
  printf("%#'.10d\n", 123); 
  printf("%#'.10d\n", 1234); 
  printf("%#'.10d\n", 12345); 
  printf("%#'.10d\n", 123456); 
  printf("%#'.10d\n", 1234567); 
  printf("%#'.10d\n", 12345678); 
  printf("%#'.10d\n", 123456789); 
  printf("%#'.10lld\n", 1234567890LL); 
  printf("%#'.10lld\n", 12345678901LL); 
  printf("%#'.10lld\n", 123456789012LL); 
  return 0; 
} 
========================================= 
 
Output in the C locale in glibc-2.3.6 is as expected: 
 
0000000001 
0000000012 
0000000123 
0000001234 
0000012345 
0000123456 
0001234567 
0012345678 
0123456789 
1234567890 
12345678901 
123456789012 
 
Output in the de_DE.UTF-8 locale in glibc-2.3.6: 
 
0000000001 
0000000012 
0000000123 
000001.234 
000012.345 
000123.456 
01.234.567 
12.345.678 
123.456.789 
1.234.567.890 
12.345.678.901 
123.456.789.012 
 
Expected output according to POSIX: 
 
0.000.000.001 
0.000.000.012 
0.000.000.123 
0.000.001.234 
0.000.012.345 
0.000.123.456 
0.001.234.567 
0.012.345.678 
0.123.456.789 
1.234.567.890 
12.345.678.901 
123.456.789.012 
 
As you can see, glibc has interpreted the precision 10 as if a _width_ of 10 
was specified, with leading 0, but because of the thousands grouping it should 
translate to a width of 13.

-- 
           Summary: printf: meaning of precision for 'd', 'i', 'o', 'u',
                    'x', 'X' directives
           Product: glibc
           Version: 2.3.6
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: drepper at redhat dot com
        ReportedBy: bruno at clisp dot org
                CC: glibc-bugs at sources dot redhat dot com
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://sourceware.org/bugzilla/show_bug.cgi?id=2194

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]