Subject: Re: Printing of off_t and size_t values
To: Martin Husemann <martin@duskware.de>
From: Klaus Klein <kleink@reziprozitaet.de>
List: tech-misc
Date: 01/20/2003 12:55:00
Martin Husemann <martin@duskware.de> writes:
> /*
> * To printf 64 bit quantities, use %ll and cast to (long long).
> */
> printf("The size of %s is %lld\n", p, (long long)sb->st_size);
>
> Nowadays we can do better. I suggest to change this into:
>
> /*
> * To printf size_t quantities, use %zd:
> */
> printf("The size of %s is %zd\n", p, sb->st_size);
(This may be a bad example item to pick on, as st_size is an off_t.)
Being unsigned, size_t should be formatted using %zu.
> /*
> * To printf off_t (or other 64 bit) quantities, use PRId64
> * from <inttypes.h>:
> */
> printf("Read error at offset %" PRId64 "\n", o);
>
> Is that OK?
> One option is to leave out the first part and use PRId64 (and
> friends) always.
Well, if we are really set to go C99/POSIX-2001 here, I think it would
be preferable not to have a 64-bit assumption but cast to intmax_t and
format using either %jd or PRIdMAX.
- Klaus