printf and size_t

R

ray.webster

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
 
R

Richard Heathfield

(e-mail address removed) said:
Should the following work *if* I have a c99 compiler please?
/* 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. :)
 
R

ray.webster

Richard said:
(e-mail address removed) said:



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


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

Richard Heathfield

(e-mail address removed) said:
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?!?

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

rayw

Richard said:
(e-mail address removed) said:

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)?

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.


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;
}
 
R

Richard Heathfield

rayw said:
Richard said:
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". :)

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!).
 
K

Keith Thompson

rayw said:
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.html> for details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top