[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gnulib] printf (_("...%zu..."), X) where X is of type size_t

From: Bruno Haible
Subject: Re: [bug-gnulib] printf (_("...%zu..."), X) where X is of type size_t
Date: Tue, 4 Oct 2005 19:11:38 +0200
User-agent: KMail/1.5

Paul Eggert wrote:
> It is curious that <inttypes.h> lacks the PRI* macros for size_t.
> I wonder why they left them out?

Neither ISO C 99 nor POSIX:2001 specify such macros.

The standards authors probably thought such macros are useless because
the size specifier 'z' already plays that role.

According to the general policy "write code according to the standards,
and wait for the non-compliant systems to disappear, rather than tailor
a solution for the broken systems", we should use "%zu", not invent a
synonym PRIuSIZE for it.

> Perhaps GNU extensions PRIoSIZE, PRIuSIZE, PRIxSIZE, PRIXSIZE could be
> added to GNU gettext.  That would mean we could write this instead:
>      printf (_("The size is %"PRIuSIZE".\n"), size);
> which would be nicer than either of the above.

I have a problem with that: The code which interprets the PRI* macros
is in both libintl and libc. It is hard to convince the libc maintainers
to add code in GNU libc whose purpose is only to deal with deficiencies
in other systems.

> printf is the biggest
> cesspool in the C library, and lots of things that C likes to sweep
> under the rug finds its way into printf somehow: floating-point
> accuracy, cancellation points, multibyte encoding errors, signal
> handling, storage allocation, wrong-sized types for the job, etc.,
> etc.  I suppose we could drag glibc's printf into gnulib, but I
> wouldn't envy the task of doing that, or maintaining the result.

But libintl already has a printf() that is enabled when the system's
printf() is too deficient. It has caused no problems so far. It also
already supports the 'z' size modifier.

But the difference between the current libintl printf() and this issue
is that the one in libintl is currently activated on platforms which
don't support "%m$n", namely

    - NetBSD 1.6
    - mingw

whereas for "%zu" it would need to be enabled on many more platforms:

    - MacOS X 10.1
    - FreeBSD 4.8
    - Solaris 7, 8, 9
    - AIX 3.2.5, 4.3, 5.1
    - HP-UX 11
    - OSF/1 5.1A
    - IRIX 6.5

And furthermore, it would need to be included into gnulib (because
when i18n is turned off, you _still_ have %zu in the format string).

What are the possible solutions?

1) Do as the gettext manual suggests:

      char buf[INT_BUFSIZE_BOUND (size_t)];
      sprintf (buf, "%" PRIuSIZE, size);
      printf (_("The size is %s.\n"), buf1);

2) Add a printf emulation to gnulib.

3) Assume that size_t is not larger than uintptr_t, and do

      #include <inttypes.h>
      printf (_("The size is %" PRIuPTR ".\n"), (uintptr_t) size);

4) Assume that size_t is not larger than 'unsigned long', and do

      printf (_("The size is %lu.\n"), (unsigned long) size);

I personally always use #4. The next-best is #3.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]