f



printf() Specification for size_t?

What printf() format specifier will allow me to print a size_t integer?

Seems like it should be there, but couldn't find it in documentation.

I'm using %ul, but that might not work on every system, I think.

Thanks.

--
David T. Ashley              (dta@e3ft.com)
http://www.e3ft.com          (Consulting Home Page)
http://www.dtashley.com      (Personal Home Page)
http://gpl.e3ft.com          (GPL Publications and Projects)
0
dta (455)
2/21/2008 2:51:02 AM
comp.lang.c 30603 articles. 3 followers. spinoza1111 (3246) is leader. Post Follow

13 Replies
385 Views

Similar Articles

[PageSpeed] 7

In article <ZbednabdyeYqeiHanZ2dnUVZ_gydnZ2d@giganews.com>,
David T. Ashley <dta@e3ft.com> wrote:
>What printf() format specifier will allow me to print a size_t integer?
>
>Seems like it should be there, but couldn't find it in documentation.
>
>I'm using %ul, but that might not work on every system, I think.

For C90, cast it to unsigned long and use %lu:
--------
printf("The size_t value is %lu\n",(unsigned long)sz);
--------
If you have C99, use %zu.

Note that %lu and %ul are not the same.


dave

-- 
Dave Vandervies                             dj3vande at eskimo dot com
Interestingly the venom isn't poisonous in the traditional sense, it's
just a very powerful relaxant. You relax to the point where you don't
breath.                --Andrew Nicholson in the scary devil monastery
0
dj3vande3 (264)
2/21/2008 3:12:54 AM
"David T. Ashley" <d...@e3ft.com> wrote:
> What printf() format specifier will allow me to print
> a size_t integer?

Under C90, you should use the largest (portable)
unsigned integer type, namely unsigned long...

  printf("%lu\n", (unsigned long) sizeof x);

Under C99, you can use the length modifier z...

  printf("%zu\n", sizeof x);

> Seems like it should be there, but couldn't find
> it in documentation.
>
> I'm using %ul,

Which will print an unsigned int value followed by an
ell.

> but that might not work on every
> system, I think.

Using unsigned long (uncluding conversion) is reasonably
forwards compatible. That said, it's possible to cater
for both worlds with some conditional includes and macros,
but it generally isn't worth the hassle since it's
relatively rare to print a size_t without knowing limits
on how big the object that it refers to can reasonably
be.

For instance, if you're dealing with objects that might
be larger than 4G, then you're more likely to be printing
the size in megabytes or gigabytes. Either way the integer
calculation is likely to be in the range of an unsigned
long.

--
Peter
0
airia (1802)
2/21/2008 3:24:51 AM
<dj3vande@csclub.uwaterloo.ca.invalid> wrote in message 
news:fpiq7m$6om$1@rumours.uwaterloo.ca...
> In article <ZbednabdyeYqeiHanZ2dnUVZ_gydnZ2d@giganews.com>,
> David T. Ashley <dta@e3ft.com> wrote:
>>What printf() format specifier will allow me to print a size_t integer?
>>
>>Seems like it should be there, but couldn't find it in documentation.
>>
>>I'm using %ul, but that might not work on every system, I think.
>
> For C90, cast it to unsigned long and use %lu:
> --------
> printf("The size_t value is %lu\n",(unsigned long)sz);
> --------
> If you have C99, use %zu.
>
> Note that %lu and %ul are not the same.

I had a brain malfunction.  I actually used %lu, which worked fine, but 
somehow in the newsgroup post I stated "%ul".

I'm aware that on my platform, "lu" works fine (naturally, I looked up the 
size_t definition before I did this).

My only fear (and the reason I made the post) is that I could envision 
platforms where the largest integer has far more bits than the address space 
of the machine.  I've never seen such a thing, but it seems it could exist.

In other words, I can't figure out why size_t _____necessarily_____ has any 
relationship to the largest integer size. 

0
dta (455)
2/21/2008 5:11:28 AM
"Peter Nilsson" <airia@acay.com.au> wrote in message 
news:288c586d-1c48-4afc-94cb-336ecc503bf4@d4g2000prg.googlegroups.com...
> "David T. Ashley" <d...@e3ft.com> wrote:
>> What printf() format specifier will allow me to print
>> a size_t integer?
>
> Using unsigned long (uncluding conversion) is reasonably
> forwards compatible. That said, it's possible to cater
> for both worlds with some conditional includes and macros,
> but it generally isn't worth the hassle since it's
> relatively rare to print a size_t without knowing limits
> on how big the object that it refers to can reasonably
> be.
>
> For instance, if you're dealing with objects that might
> be larger than 4G, then you're more likely to be printing
> the size in megabytes or gigabytes. Either way the integer
> calculation is likely to be in the range of an unsigned
> long.

I had a brain malfunction.  I actually used %lu, which worked fine, but 
somehow in the newsgroup post I stated "%ul".

I'm aware that on my platform, "lu" works fine (naturally, I looked up the 
size_t definition before I did this).

My only fear (and the reason I made the post) is that I could envision 
platforms where the largest integer has far more bits than the address space 
of the machine.  I've never seen such a thing, but it seems it could exist.

In other words, I can't figure out why size_t _____necessarily_____ has any 
relationship to the largest integer size. 

0
dta (455)
2/21/2008 5:12:31 AM
David T. Ashley wrote:
> What printf() format specifier will allow me to print a size_t integer?
> 
> Seems like it should be there, but couldn't find it in documentation.
> 
> I'm using %ul, but that might not work on every system, I think.

"%ul" should never work, while "%lu" often will.

If you have a library supporting the C99 specifier "%zu", use that.
Otherwise, you can't get much safer than
      "%llu" with a cast to (unsigned long long)
If your implementation supports neither, then use
      "%lu" with a cast to (unsigned long)
and pray.

0
mambuhl (2202)
2/21/2008 6:04:55 AM
Martin Ambuhl said:

> David T. Ashley wrote:
>> What printf() format specifier will allow me to print a size_t integer?
>> 
>> Seems like it should be there, but couldn't find it in documentation.
>> 
>> I'm using %ul, but that might not work on every system, I think.
> 
> "%ul" should never work, while "%lu" often will.

I see no reason why %ul should not work. There is nothing to stop 
implementors from using unsigned ints as the underlying type for size_t, 
and there is nothing to stop programmers from putting an 'l' onto stdout 
immediately after the size value.

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
0
rjh (10790)
2/21/2008 8:00:28 AM
"David T. Ashley" <dta@e3ft.com> writes:
> <dj3vande@csclub.uwaterloo.ca.invalid> wrote in message
> news:fpiq7m$6om$1@rumours.uwaterloo.ca...
>> In article <ZbednabdyeYqeiHanZ2dnUVZ_gydnZ2d@giganews.com>,
>> David T. Ashley <dta@e3ft.com> wrote:
>>>What printf() format specifier will allow me to print a size_t integer?
>>>
>>>Seems like it should be there, but couldn't find it in documentation.
>>>
>>>I'm using %ul, but that might not work on every system, I think.
>>
>> For C90, cast it to unsigned long and use %lu:
>> --------
>> printf("The size_t value is %lu\n",(unsigned long)sz);
>> --------
>> If you have C99, use %zu.
>>
>> Note that %lu and %ul are not the same.
>
> I had a brain malfunction.  I actually used %lu, which worked fine,
> but somehow in the newsgroup post I stated "%ul".
>
> I'm aware that on my platform, "lu" works fine (naturally, I looked up
> the size_t definition before I did this).
>
> My only fear (and the reason I made the post) is that I could envision
> platforms where the largest integer has far more bits than the address
> space of the machine.  I've never seen such a thing, but it seems it
> could exist.
>
> In other words, I can't figure out why size_t _____necessarily_____
> has any relationship to the largest integer size. 

size_t is necessarily no bigger than the largest integer size.
There's no guarantee that it's the same as any particular predefined
integer type, which is why (unless you have "%zu") you have to convert
it to some type to print it.

In C90, the largest unsigned integer type (size_t is guaranteed to be
an unsigned type) is unsigned long, so this:

    size_t size = <whatever>;
    printf("size = %lu\n", (unsigned long)size);

should always work.  In C99, unsigned long long isn't guaranteed to be
the widest unsigned integer type (an implementation can provide
extended integer types), and it's conceivable that size_t could be
wider than unsigned long.  It's guaranteed that size_t is no wider
than uintmax_t, but since C99 provides formats for both size_t and
uintmax_t that isn't all that helpful.

If you need portability to both C90 and C99 implementations, you can
still use the "%lu" trick *if* you can guarantee that the particular
value you're printing is no greater than ULONG_MAX.  Even if size_t is
wider than unsigned long, if the value doesn't exceed ULONG_MAX then
the cast performs a (narrowing) conversion that doesn't lose any
information.  (Even if the value is greater than ULONG_MAX, the
semantics of unsigned types means that you'll just get an incorrect
result; there's no undefined behavior.)

If you know your code will run only under conforming C99
implementations, you can use "%zu" (and there's no need for a cast).
But note that, out in the real world, even testing for
__STDC_VERSION__ >= 199901L doesn't necessarily guarantee that "%zu"
will work.  The __STDC_VERSION__ macro is set by the compiler; it's up
to the runtime library to implement printf properly.  An
implementation combining a compiler and a runtime library *should* get
this right, but I think that some don't.

-- 
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
kst-u (21963)
2/21/2008 8:21:28 AM
Richard Heathfield wrote:
> Martin Ambuhl said:
> 
>> David T. Ashley wrote:
>>> What printf() format specifier will allow me to print a size_t integer?
>>>
>>> Seems like it should be there, but couldn't find it in documentation.
>>>
>>> I'm using %ul, but that might not work on every system, I think.
>> "%ul" should never work, while "%lu" often will.
> 
> I see no reason why %ul should not work. There is nothing to stop 
> implementors from using unsigned ints as the underlying type for size_t, 
> and there is nothing to stop programmers from putting an 'l' onto stdout 
> immediately after the size value.

Thank you for your valuable contribution.

0
mambuhl (2202)
2/21/2008 9:14:58 AM
Martin Ambuhl said:

> Richard Heathfield wrote:
>> Martin Ambuhl said:
>> 
>>> David T. Ashley wrote:
>>>> What printf() format specifier will allow me to print a size_t
>>>> integer?
>>>>
>>>> Seems like it should be there, but couldn't find it in documentation.
>>>>
>>>> I'm using %ul, but that might not work on every system, I think.
>>> "%ul" should never work, while "%lu" often will.
>> 
>> I see no reason why %ul should not work. There is nothing to stop
>> implementors from using unsigned ints as the underlying type for size_t,
>> and there is nothing to stop programmers from putting an 'l' onto stdout
>> immediately after the size value.
> 
> Thank you for your valuable contribution.

You're quite welcome, my dear chap!

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
0
rjh (10790)
2/21/2008 10:33:37 AM
Richard Heathfield wrote:
> Martin Ambuhl said:
>> David T. Ashley wrote:
>>
>>> What printf() format specifier will allow me to print a size_t
>>> integer?  Seems like it should be there, but couldn't find it
>>> in documentation.  I'm using %ul, but that might not work on
>>> every system, I think.
>>
>> "%ul" should never work, while "%lu" often will.
> 
> I see no reason why %ul should not work. There is nothing to
> stop implementors from using unsigned ints as the underlying
> type for size_t, and there is nothing to stop programmers from
> putting an 'l' onto stdout immediately after the size value.

I think this will create great confusion for newbies.  It should be
labelled, etc. to attempt to avoid being taken too seriously.

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.



-- 
Posted via a free Usenet account from http://www.teranews.com

0
cbfalconer (19194)
2/21/2008 1:53:54 PM
On Feb 21, 3:53 pm, CBFalconer <cbfalco...@yahoo.com> wrote:
> Richard Heathfield wrote:
> > Martin Ambuhl said:
> >> David T. Ashley wrote:
>
> >>> What printf() format specifier will allow me to print a size_t
> >>> integer?  Seems like it should be there, but couldn't find it
> >>> in documentation.  I'm using %ul, but that might not work on
> >>> every system, I think.
>
> >> "%ul" should never work, while "%lu" often will.
>
> > I see no reason why %ul should not work. There is nothing to
> > stop implementors from using unsigned ints as the underlying
> > type for size_t, and there is nothing to stop programmers from
> > putting an 'l' onto stdout immediately after the size value.
>
> I think this will create great confusion for newbies.  It should be
> labelled, etc. to attempt to avoid being taken too seriously.
It would also confuse a newbie to think that %ul would print an
unsigned long.
When printf() encounters a % in the string literal, it searches for
one of the conversion specifiers, which are
d,i,a,u,x,X,f,F,e,E,g,G,a,A,c,s,p,n,%, and also parses any flag
characters, length modifiers, precision and field width which are
optional.
When not doing that, printf() simply puts the bytes to stdout.
So, according to that, %ul would be parsed as:
%: start parsing
u: valid conversion specifier. stop parsing
l: byte. put (write) to stdout

Hope this clears any confusion a newbie might had.

Reference: 7.19.6.1 ISO C99
0
vippstar (1211)
2/21/2008 3:10:18 PM
On 21 Feb 2008 at 10:33, Richard Heathfield wrote:
> Martin Ambuhl said:
>
>> Richard Heathfield wrote:
>>> Martin Ambuhl said:
>>> 
>>>> David T. Ashley wrote:
>>>>> What printf() format specifier will allow me to print a size_t
>>>>> integer?
>>>>>
>>>>> Seems like it should be there, but couldn't find it in documentation.
>>>>>
>>>>> I'm using %ul, but that might not work on every system, I think.
>>>> "%ul" should never work, while "%lu" often will.
>>> 
>>> I see no reason why %ul should not work. There is nothing to stop
>>> implementors from using unsigned ints as the underlying type for size_t,
>>> and there is nothing to stop programmers from putting an 'l' onto stdout
>>> immediately after the size value.
>> 
>> Thank you for your valuable contribution.
>
> You're quite welcome, my dear chap!

Ouch! Twice the sarcasm cuts like a knife as we one again play "clc
regulars eating their own"...

0
nospam59 (11087)
2/21/2008 6:38:41 PM
David T. Ashley wrote:
> 
> <dj3vande@csclub.uwaterloo.ca.invalid> wrote in message
> news:fpiq7m$6om$1@rumours.uwaterloo.ca...
> > In article <ZbednabdyeYqeiHanZ2dnUVZ_gydnZ2d@giganews.com>,
> > David T. Ashley <dta@e3ft.com> wrote:
> >>What printf() format specifier
> >>will allow me to print a size_t integer?
> >>
> >>Seems like it should be there,
> >>but couldn't find it in documentation.
> >>
> >>I'm using %ul, but that might not work on every system, I think.
> >
> > For C90, cast it to unsigned long and use %lu:
> > --------
> > printf("The size_t value is %lu\n",(unsigned long)sz);
> > --------
> > If you have C99, use %zu.
> >
> > Note that %lu and %ul are not the same.
> 
> I had a brain malfunction.
> I actually used %lu, which worked fine, but
> somehow in the newsgroup post I stated "%ul".

That's why I call it (long unsigned) instead of (unsigned long).

-- 
pete
0
pfiland (6614)
2/21/2008 9:40:46 PM
Reply: