On 17 Oct 2012, at 09:26, Phil Mayers <p.mayers@imperial.ac.uk> wrote:
On 10/17/2012 09:04 AM, Brian Candler wrote:
Aside: this uses "%u", sizeof(...) for the first argument. Is it guaranteed that sizeof() is int on all platforms?
Very much not.
Quite.
sizeof returns size_t which is always unsigned (so never "int"), but otherwise implementation (compiler) dependent. In principle "%zu" is the correct format spec. However, I'm not sure how widely supported that is on older compilers - even on current ones.
%zu was specified in C99, it is by no means new.
I guess if there's any doubt,
"%lu", (long)sizeof(...)
should be pretty safe.
Yuck. Cast of unsigned to signed :oP
TBH it's a fairly theoretical argument; sizeof is used on objects whose size tends to be in the range 1-100k and specified at compile time. The odds of sizeof ever returning >2**31 are very, very remote, at least with current system architectures.
:) -Arran