but VC++9 produces "negative
subscript" error while compiling that
- sizeof( UCHAR_MAX ) happens to be 4, not 1.
With this post I am not answering in terms of the solution, but trying to get to the root cause.
Consider the expression
#define MAX 255;
My understanding is that sizeof(MAX) a.k.a (sizeof(255))
is always going to be always the size of the integer literal on the given platform as per the given rules in the Standard 2.3.1/2. Just because it is UCHAR_MAX and holds the max value of a char
does not mean that the size of such a name will be the size of char
The type of an integer literal depends
on its form, value, and suffix. If it
is decimal and has no suffix, it has
the first of these types in which its
value can be represented: int, long
int; if the value cannot be
represented as a long int, the
behavior is undefined. If it is octal
or hexadecimal and has no suffix, it
has the first of these types in which
its value can be represented: int,
unsigned int, long int, unsigned long
int. If it is suffixed by u or U, its
type is the first of these types in
which its value can be represented:
unsigned int, unsigned long int. If it
is suffixed by l or L, its type is the
first of these types in which its
value can be represented: long int,
unsigned long int. If it is suffixed
by ul, lu, uL, Lu, Ul, lU, UL, or LU,
its type is unsigned long int.
So the expectation of it being 1
needs to be rechecked which seems to the root cause here. sizeof(MAX)
will be one only on those architectures where int
is as wide as a char
. I am not sure how many such systems are really out there.