Skarmander wrote On 10/05/05 10:30,:
> Eric Sosman wrote:
>
>>Sunil wrote:
>>
>>> printf("char : %d\n",sizeof(char));
>>> [...]
>>
>> On some systems I have used, the output would claim
>>that all the types are of size zero. Hint: What type of
>>value does "%d" expect, and what type of value does sizeof
>>produce?
>>
> <snip>
> What exactly *is* the format specifier for size_t in C90? C99 has "%zu",
> but is (say) "%lu" guaranteed to work?
The usual C90 way is
printf ("size = %lu\n", (unsigned long)sizeof(Type));
This works because size_t must be an unsigned integer type,
C90 has only four such types, and unsigned long can handle
all the values of any of the four.
In C99 the number of unsigned integer types is much
larger, and varies from one implementation to another. The
widest unsigned integer type is uintmax_t, so one could
write (using another C99-invented length modifier)
printf ("size = %ju\n", (uintmax_t)sizeof(Type));
I do not know for sure why the committee decided to
invent the "z" width modifier, but two motivations seem
plausible:
- That silly cast is a pain, and since other length
modifiers were already being invented it was easy
to introduce a new one for size_t.
- On small machines size_t might be as narrow as 16
bits, while uintmax_t must be at least 64 bits.
Working with 64-bit values might require a multi-
precision software library that would otherwise
not be needed. The "z" modifier lets one avoid
using uintmax_t, and might allow the implementation
to exclude the unnecessary library (recall systems
that tried to omit software floating-point support
when they thought the program wouldn't use it.)
--