Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Portable way to printf() a size_t instance

Reply
Thread Tools

Portable way to printf() a size_t instance

 
 
Joakim Hove
Guest
Posts: n/a
 
      06-12-2005

Hello,

I have code which makses use of variables of type size_t. The code is
originally developed on a 32 bit machine, but now it is run on both a
32 bit and a 64 bit machine.

In the code have statements like this:

size_t buffer_size;
printf("Total buffer size: %ud bytes \n",buffer_size);

Now the format "%ud" works nicely on the 32 bit computer; it actually
works on the 64 bit computer as well, but the compiler spits out
warning message. On the 64 bit computer it would have prefered:

printf("Total buffer size: %uld bytes \n",buffer_size); /* Or udl? */

As I said it works, but I would really prefer the program to compile
without warnings, as it is now I get *many* warnings of the type

file.c:line: warning: unsigned int format, different type arg (arg 1).

And, on several occasion this has actually led me to miss more
important warnings.


Any tips appreciated.


Joakim


--
Joakim Hove
hove AT ift uib no /
Tlf: +47 (55 5)8 27 90 / Stabburveien 18
Fax: +47 (55 5)8 94 40 / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      06-12-2005
Joakim Hove wrote:
>
> Hello,
>
> I have code which makses use of variables of type size_t. The code is
> originally developed on a 32 bit machine, but now it is run on both a
> 32 bit and a 64 bit machine.
>
> In the code have statements like this:
>
> size_t buffer_size;
> printf("Total buffer size: %ud bytes \n",buffer_size);
>
> Now the format "%ud" works nicely on the 32 bit computer; it actually
> works on the 64 bit computer as well, but the compiler spits out
> warning message. On the 64 bit computer it would have prefered:
>
> printf("Total buffer size: %uld bytes \n",buffer_size); /* Or udl? */
>
> As I said it works, but I would really prefer the program to compile
> without warnings, as it is now I get *many* warnings of the type
>
> file.c:line: warning: unsigned int format, different type arg (arg 1).
>
> And, on several occasion this has actually led me to miss more
> important warnings.
>
> Any tips appreciated.


For C89, convert to long unsigned.

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

--
pete
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-12-2005
Joakim Hove wrote:
> Hello,
>
> I have code which makses use of variables of type size_t. The code is
> originally developed on a 32 bit machine, but now it is run on both a
> 32 bit and a 64 bit machine.
>
> In the code have statements like this:
>
> size_t buffer_size;
> printf("Total buffer size: %ud bytes \n",buffer_size);
>
> Now the format "%ud" works nicely on the 32 bit computer;


It shouldn't. The %<qualifier>d are the conversion specifiers for signed
integers; the %<qualifier>u are the conversion specifiers for unsigned
integers. 'u' is not a qualifier for %d.


> it actually
> works on the 64 bit computer as well, but the compiler spits out
> warning message.


Something like "%ud doesn't mean anything"?

> On the 64 bit computer it would have prefered:
>
> printf("Total buffer size: %uld bytes \n",buffer_size); /* Or udl? */


I doubt it. The printf routine rarely "wants" meaningless specifiers.
If you have an up-to-date library, you could use
printf("Total buffer size: %zu bytes \n",buffer_size);

Otherwise, try
printf("Total buffer size: %lu bytes \n",
(long unsigned)buffer_size);
or
printf("Total buffer size: %llu bytes \n",
(long long unsigned) buffer_size);

That is, use an (properly written) unsigned specifier and cast the
size_t argument to the same size unsigned.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-12-2005
Martin Ambuhl wrote (and now clarifies):
> Joakim Hove wrote:

[...]
>> Now the format "%ud" works nicely on the 32 bit computer;


> It shouldn't. The %<qualifier>d are the conversion specifiers for signed
> integers; the %<qualifier>u are the conversion specifiers for unsigned
> integers. 'u' is not a qualifier for %d.


I should have been clearer. "%u" is the specifier you are using. The
"d" is a literal portion of the output string. See the example below
and note the extra 'd' at the end of the string:

#include <stdio.h>
int main(void)
{
printf("Printing an unsigned value using %%ud: %ud\n", 5u);
return 0;
}

Printing an unsigned value using %ud: 5d
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-12-2005
Joakim Hove <> writes:
> I have code which makses use of variables of type size_t. The code is
> originally developed on a 32 bit machine, but now it is run on both a
> 32 bit and a 64 bit machine.
>
> In the code have statements like this:
>
> size_t buffer_size;
> printf("Total buffer size: %ud bytes \n",buffer_size);


I think you mean "%u", not "%ud". "%d" is for (signed) int; "%u" is
for unsigned int.

> Now the format "%ud" works nicely on the 32 bit computer; it actually
> works on the 64 bit computer as well, but the compiler spits out
> warning message. On the 64 bit computer it would have prefered:
>
> printf("Total buffer size: %uld bytes \n",buffer_size); /* Or udl? */


The format for unsigned long is "%lu".

In C90, the best way to print a size_t value is to convert it
to unsigned long and use "%lu":

printf("Total buffer size: %lu bytes\n", (unsigned long)buffer_size);

C99 adds a 'z' modifier specifically for size_t:

printf("Total buffer size: %zu bytes\n", buffer_size);

but many printf implementations don't support it. (Even if your
compiler supports C99 and defines __STDC_VERSION__ appropriately,
that's not, practically speaking, a guarantee that the library also
conforms to C99.)

Even in C99, the "%lu" method will work unless size_t is bigger than
unsigned long *and* the value being printed exceeds ULONG_MAX, which
is unlikely to happen in practice.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
Chris Croughton
Guest
Posts: n/a
 
      06-12-2005
On Sun, 12 Jun 2005 08:07:07 +0200, Joakim Hove
<> wrote:

> I have code which makses use of variables of type size_t. The code is
> originally developed on a 32 bit machine, but now it is run on both a
> 32 bit and a 64 bit machine.
>
> In the code have statements like this:
>
> size_t buffer_size;
> printf("Total buffer size: %ud bytes \n",buffer_size);


That is undefined, because %u expects an int. Quite why you want to
print a 'd' after the number I don't know, but it's your choice...

> Now the format "%ud" works nicely on the 32 bit computer; it actually
> works on the 64 bit computer as well, but the compiler spits out
> warning message. On the 64 bit computer it would have prefered:
>
> printf("Total buffer size: %uld bytes \n",buffer_size); /* Or udl? */


You probably mean %lu -- but it's still undefined behaviour because
size_t is not the same as an unsigned long except by accident (well,
implementers' choice, but something over which you have no control and
the next version of the compiler could change it).

> As I said it works, but I would really prefer the program to compile
> without warnings, as it is now I get *many* warnings of the type
>
> file.c:line: warning: unsigned int format, different type arg (arg 1).


Heed them. Your code is incorrect. Try, for instance,

size_t buffer_size = 123;
printf("%u %d\n", buffer_size, 42);

On the 32 bit machine it may work (and give "123 42"), but on the 64 bit
machine it may give you something like "123 0" (or possibly "0 123", or
some other strange result).

> And, on several occasion this has actually led me to miss more
> important warnings.


So correct the printf code.

If your library is C99 compliant you can use the z conversion specifier
for size_t:

printf("%zu\n", buffer_size);

Note that the /library/ must be compliant, not the actual compiler, and
since gcc for instance uses printf from the underlying system it may not
be supported (as I recall it was on Linux but wasn't on Solaris when I
tried it). If the compiler doesn't support it then you may still get
warnings, of course.

Alternatively, cast the size to a known and supported type. For most
purposes long is sufficient:

printf("%lu\n", (long)buffer_size);

That will get rid of the warnings (because there is no longer any
mismatch) and allow for at least 32 bit sizes even on 16 bit machines.

Chris C
 
Reply With Quote
 
Me
Guest
Posts: n/a
 
      06-12-2005
%z is the specifier for size_t in C99 but I assume you want to be
backwards compatible with older and broken implementations. Here is the
smart way to go about this (untested, use at your own risk):

#if __STDC_VERSION__ >= 199901L

#include <stdint.h>

#else

#include <limits.h>

#ifdef LLONG_MAX
typedef long long intmax_t;
typedef unsigned long long uintmax_t;
#define INTMAX_MIN LLONG_MIN
#define INTMAX_MAX LLONG_MAX
#define UINTMAX_MAX ULLONG_MAX
#elif defined(_I64_MAX)
typedef __int64 intmax_t;
typedef __uint64 uintmax_t;
#define INTMAX_MIN _I64_MIN
#define INTMAX_MAX _I64_MAX
#define UINTMAX_MAX _UI64_MAX
#else
typedef long intmax_t;
typedef unsigned long uintmax_t;
#define INTMAX_MIN LONG_MIN
#define INTMAX_MAX LONG_MAX
#define UINTMAX_MAX ULONG_MAX
#endif

#endif

or something similar for an implementation specific [u]intmax_t type
somewhat portably defined. There are several ways to do the next part
but here is one simple way is to create another implementation specific
header file:

#if __STDC_VERSION__ >= 199901L

#include <inttypes.h>

#else

#ifdef LLONG_MAX
#define PRIiMAX "%lli"
#define PRIuMAX "%llu"
#elif defined(_I64_MAX)
#define PRIiMAX "%I64i"
#define PRIuMAX "%I64u"
#else
#define PRIiMAX "%li"
#define PRIuMAX "%lu"
#endif

#endif

And for all your printf calls do:

printf("Total size: " PRIuMAX " bytes\n", (uintmax_t)size);

Another simple way is to create printf specifier for size_t similar to
the above using another implementation specific header that you have to
keep in sync with: "%z" for C99, "%Iu" for visual studio's runtime,
etc. or however you want to go about doing it.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-12-2005
Keith Thompson wrote:
> Joakim Hove <> writes:
>
>> I have code which makses use of variables of type size_t. The
>> code is originally developed on a 32 bit machine, but now it is
>> run on both a 32 bit and a 64 bit machine.
>>
>> In the code have statements like this:
>>
>> size_t buffer_size;
>> printf("Total buffer size: %ud bytes \n",buffer_size);

>
> I think you mean "%u", not "%ud". "%d" is for (signed) int; "%u"
> is for unsigned int.
>
>> Now the format "%ud" works nicely on the 32 bit computer; it
>> actually works on the 64 bit computer as well, but the compiler
>> spits out warning message. On the 64 bit computer it would have
>> prefered:
>>
>> printf("Total buffer size: %uld bytes \n",buffer_size); /* Or udl? */

>
> The format for unsigned long is "%lu".
>
> In C90, the best way to print a size_t value is to convert it
> to unsigned long and use "%lu":
>
> printf("Total buffer size: %lu bytes\n", (unsigned long)buffer_size);
>
> C99 adds a 'z' modifier specifically for size_t:
>
> printf("Total buffer size: %zu bytes\n", buffer_size);
>
> but many printf implementations don't support it. (Even if your
> compiler supports C99 and defines __STDC_VERSION__ appropriately,
> that's not, practically speaking, a guarantee that the library
> also conforms to C99.)
>
> Even in C99, the "%lu" method will work unless size_t is bigger
> than unsigned long *and* the value being printed exceeds ULONG_MAX,
> which is unlikely to happen in practice.


I was going to reply to the OP, but this covers it better. The
major point is that, for C89 or any lack of full printf C99
coverage in the library, you have to cast the size_t operand to
something, and you have to then make the specifier agree with that
cast. The programmer probably knows how bit his size_t object can
actually become, and so can make the decision about what to cast it
to. The safest is the largest unsigned that the library can
handle.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
ade ishs
Guest
Posts: n/a
 
      06-12-2005
Chris Croughton wrote:

> Alternatively, cast the size to a known and supported type. For most
> purposes long is sufficient:
>
> printf("%lu\n", (long)buffer_size);


ITYM

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

--
ade ishs

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-12-2005
Chris Croughton <> writes:
> On Sun, 12 Jun 2005 08:07:07 +0200, Joakim Hove
> <> wrote:
>
>> I have code which makses use of variables of type size_t. The code is
>> originally developed on a 32 bit machine, but now it is run on both a
>> 32 bit and a 64 bit machine.
>>
>> In the code have statements like this:
>>
>> size_t buffer_size;
>> printf("Total buffer size: %ud bytes \n",buffer_size);

>
> That is undefined, because %u expects an int. Quite why you want to
> print a 'd' after the number I don't know, but it's your choice...

[...]

I think it's only potentially undefined. If size_t happens to be
unsigned int for a given implementation, it's well defined. If you
use a "%u" format, the implementation doesn't care whether you
actually know that the argument is an unsigned int, or you just got
lucky.

It is, of course, unnecessarily non-portable.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Portable Python - free portable development environment ! perica.zivkovic@gmail.com Python 7 01-13-2007 11:19 AM
what's the 'right' way to get rid of size_t to int conversion warnings? glen stark C++ 3 03-21-2006 09:36 PM
portable (VHDL) vs. non-portable (altera LPM) approaches to signed computations Eli Bendersky VHDL 1 03-01-2006 02:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57