Richard Heathfield said:
(e-mail address removed), India said:
The code in the original article was indented; the indentation was
lost in Richard's followup, at least as I saw it. Probably something
somewhere doesn't like tab characters. Try to use only spaces for
indentation when posting to Usenet. (I've maually re-indented the
code here.)
No, because there is no guarantee that size_t has the same size as any
other integer type. But you can do this:
#include <stddef.h>
int main(void)
{
size_t size = (size_t)-1;
return 0;
}
You're right, of coures, that (size_t)-1 is a better solution than the
if/else chain above. A quibble, however: size_t *is* guaranteed to
have the same size as some other integer type. It's a typedef, and it
has to be an alias for something. In C99, however, it could be an
alias for an extended integer type rather than for one of the standard
predefined integer types. (I suppose a C90 implementation could
provide such types as an extension.)
In C90, the type "unsigned long long" doesn't exist (though it's a
common extension). You can either drop "unsigned long long" and have
the code break on implementations where unsigned long long is bigger
than unsigned long and size_t is an alias for unsigned long long, *or*
you can include it and have the code break on systems that don't
support unsigned long long. The __STDC_VERSION__ macro should tell
you what version of C is supported, but there are plenty of
implementations that support unsigned long long without supporting all
of C99.
Implementations are encouraged *not* to make size_t bigger than
unsigned long long unless it's actually necessary. (This is in n1124,
but not in the original C99 standard.) But this doesn't do you much
good.
It's conceivable, though vanishingly unlikely, that size_t is an alias
for unsigned short, or even unsigned char. Perhaps the DS9K does
this.
It's also possible for unsigned int and unsigned long to have the same
size, but for unsigned long to have a wider range (if unsigned int has
padding bits). If size_t on such a system is an alias for unsigned
long, then the above code will quietly produce an incorrect result.
In C99, <stdint.h> defines the SIZE_MAX macro, which is exactly what
you're looking for, but not all implementations provide it.