469,317 Members | 1,895 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,317 developers. It's quick & easy.

printf and size_t

Should the following work *if* I have a c99 compiler please?

#include <stdio.h>

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 compiler - use lu.
*/
printf("%lu", (unsigned long)t);

# endif

return 0;
}

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?

BTW, where would you use plain ol' %z, vs. %zu, i.e, for what data
type?

Ta,

rayw

Aug 8 '06 #1
6 15980
ra*********@gmail.com said:
Should the following work *if* I have a c99 compiler please?
<snip>
>
/* C99, so it should support %zu?
*/
printf("%zu", t);
Yes, that's correct, although you might want to stick a \n in there.
# else

/* Not a C99 compiler - use lu.
*/
printf("%lu", (unsigned long)t);

# endif

return 0;
}

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.
BTW, where would you use plain ol' %z, vs. %zu, i.e, for what data
type?
You wouldn't. In C99's *printf, z is a length modifier like h or l, not a
conversion specifier like d or u. And in pre-C99, it's a bug. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 8 '06 #2

Richard Heathfield wrote:
ra*********@gmail.com said:
Should the following work *if* I have a c99 compiler please?
<snip>

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

Yes, that's correct, although you might want to stick a \n in there.
Yup.
# else

/* Not a C99 compiler - use lu.
*/
printf("%lu", (unsigned long)t);

# endif

return 0;
}

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.
BTW, where would you use plain ol' %z, vs. %zu, i.e, for what data
type?

You wouldn't. In C99's *printf, z is a length modifier like h or l, not a
conversion specifier like d or u. And in pre-C99, it's a bug. :-)
Duh - should have seen that! Thanks.

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?

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?

Many thanks,

rayw

Aug 8 '06 #3
ra*********@gmail.com said:
Richard Heathfield wrote:
>ra*********@gmail.com 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?!?

<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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 8 '06 #4

Richard Heathfield wrote:
ra*********@gmail.com said:
Richard Heathfield wrote:
ra*********@gmail.com 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;
}

Aug 8 '06 #5
rayw said:
Richard Heathfield wrote:
>>
Hold on there - what makes you think the compiler is C99-conforming?!?

Because [...] it looks as though my gcc says that it's c99
compliant
Consider the difference between "says that it is" and "is". :-)

<snip>
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);
We already know this doesn't work if the libc is not conforming (see
upthread).
>
# else

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

# if sizeof(size_t) <= sizeof(unsigned long)
Alas, sizeof is a C operator, not a preprocessor directive. This won't
compile (or rather, it won't preprocess!).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 8 '06 #6
"rayw" <ra*********@gmail.comwrites:
Richard Heathfield wrote:
>ra*********@gmail.com said:
Richard Heathfield wrote:
ra*********@gmail.com 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)?
Yes, __STDC_VERSION__ is defined by the compiler, not by the runtime
library. The standard doesn't actually say so; the compiler and the
runtime library are both part of the implementation.

gcc itself is just a compiler; it uses whatever runtime library is
available on the system. __STDC_VERSION__ *should* reflect the
conformance of the implementation as a whole. Theoretically, gcc
could detect what runtime library it's using, and set __STDC_VERSION__
accordingly, but as far as I know it doesn't.

So if __STDC_VERSION__ is 199901L, but the implementation doesn't
support "%zu", that's a bug in the implementation.

Note also that gcc itself doesn't support all of C99; see
<http://gcc.gnu.org/c99status.htmlfor details.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 8 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bilbo | last post: by
6 posts views Thread by David Mathog | last post: by
5 posts views Thread by diadia | last post: by
25 posts views Thread by Joakim Hove | last post: by
16 posts views Thread by Jordan Abel | last post: by
25 posts views Thread by Podrzut_z_Laweczki | last post: by
11 posts views Thread by vectorizor | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.