Re: printf and size_t




Richard Heathfield wrote:
ray.webster@xxxxxxxxx said:
Richard Heathfield wrote:
ray.webster@xxxxxxxxx said:

I'm using gcc 3.4.2 and the output is zu, this, I believe, shows that
the printf supplied doesn't recognize %zu as a valid format specifier?

Yes, it shows that the libc you are using is not C99-conforming.

Ah, so the gcc I'm using hasn't come complete with a libc that's also
compliant.

Hold on there - what makes you think the compiler is C99-conforming?!?

Because this ...

# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

/* C99, so it should support %zu?
*/
printf("%zu", t);

...

outputs 'zu' - so it looks as though my gcc says that it's c99
compliant (__STDC_VERSION__ isn't defined in stdio.h (which is all I
have included) so I'm assuming it's defined 'internally' by the
compiler - which certainly sounds like it ought to work that way, e.g.,
that you 'ask' the compiler rather that 'trust' an include file)?


<snip>

So, if %zu isn't supported, and I can't get a libc that does support
it, is %lu ok - or - should I use unsigned long long?

Use: printf("%lu\n", (unsigned long)sizeof object);

unless you are likely to be dealing with objects greater than 4294967295
bytes in size, in which case make sure first that unsigned long is big
enough. If you are and it isn't, you may be forced to use unsigned long
long if your compiler supports it.

Even then, as far as I know, there's no way to know whether even that
has enough bits to hold a size_t (right?) - should I check that with an
assertion say - that sizeof(size_t) <= sizeof(unsigned long) etc?

Well, that's why you need to cast - because printf is variadic, the
arguments you supply to it won't automagically be converted, so you have to
be very careful with types. But provided the /value/ fits inside an
unsigned long, the cast is necessary and sufficient.

Does this look ok?

int main(void)
{
size_t t = 42;

# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

/* C99, so it should support %zu?
*/
printf("%zu", t);

# else

/* Not a C99.
**
** Use unsigned long OR unsigned long long instead.
*/

# if sizeof(size_t) <= sizeof(unsigned long)

printf("%lu", (unsigned long)t);

# else

printf("%llu", (unsigned long long)t);

# endif

# endif

return 0;
}

.



Relevant Pages

  • [RFC][PATCH-2.6] Clean up and merge compiler-*.h
    ... the kernel headers in include/linux to include/linux-abi. ... * Common definitions for all gcc versions go here. ... -/* Some compiler specific definitions are overwritten here ...
    (Linux-Kernel)
  • Re: float.h and math.h
    ... The hex floats are "new" in C99 aren't they..... ... That's because it's not a constant expression. ... And as GCC 331 is still C89 I guess that the sort of fudges I am using ... So I need a "C" compiler that will, at least cross compile to the I370 (not ...
    (comp.lang.c)
  • RE: [Patch] asm workarounds in generic header files
    ... And intel.diff patch adds Intel compiler specific definitions. ... * Common definitions for all gcc versions go here. ... +/* Optimization barrier */ ...
    (Linux-Kernel)
  • Re: Linux Kernel include files
    ... And Joerg said the Sun cc supported "long long" before gcc did. ... That way if you have any kind of vaguely-long-long-compatible compiler then ... There had been need of rain for many days. ...
    (Linux-Kernel)
  • Re: C99 IDE for windows
    ... Hardly anyone has access to a C99 implementation. ... Gnu has relatively minor failures in C99 ... that gcc offers -- but what if your users want to compile your code on ... different compiler because it generates faster code for their system ...
    (comp.lang.c)