[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[patch #5727] fix size_t format specifiers

From: Ben Pfaff
Subject: [patch #5727] fix size_t format specifiers
Date: Thu, 08 Feb 2007 05:54:49 +0000
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20061205 Iceweasel/ (Debian-

Follow-up Comment #3, patch #5727 (project pspp):

>1. Any sane implementation of printf would cast to the appropriate
>type internally. 

The implementation of printf has little to do with it.  The compiler passes
arguments to printf as the types that are supplied.  It doesn't know what
format specifier(s) are given.  Thus, if you pass a 64-bit size_t for a %d
argument, and on that machine "int" is 32 bits, then what you print is half

Now, GCC is a little smarter and knows about printf, but that doesn't change
how it passes arguments, it just allows it to print out helpful warnings.

>2. In some cases you're casting from size_t to int. If size_t on a
>particular achitecture is 64 bits, and int is 32bits, then 
>confusing things could happen. 

The changes I made all have the effect of making the argument match the
format specifier: if I cast to int, then I use %d; if I cast to unsigned
long, then I use %lu; and so on.  Arguably I could have chosen different
types in some cases, but I tried to choose what made sense to me at the

I'm hoping that someday gnulib will replace printf-like functions with
versions that accept the "z" format specifier (which was introduced in C99
for printing size_t), so that instead of these casts we can just write "%zu".
 But that day is not here yet.


Reply to this item at:


  Message sent via/by Savannah

reply via email to

[Prev in Thread] Current Thread [Next in Thread]