In ctype.h, line 20, __ismask
is defined as:
#define __ismask(x) (_ctype[(int)(unsigned char)(x)])
What does (int)(unsigned char)(x)
do? I guess it casts x
to unsigned char (to retrieve the first byte only regardless of x
), but then why is it cast to an int
at the end?
(unsigned char)(x)
effectively computes an unsigned char
with the value of x % (UCHAR_MAX + 1)
. This has the effect of giving a positive value (between 0
and UCHAR_MAX
). With most implementations UCHAR_MAX
has a value of 255
(although the standard permits an unsigned char
to support a larger range, such implementations are uncommon).
Since the result of (unsigned char)(x)
is guaranteed to be in the range supported by an int
, the conversion to int
will not change value.
Net effect is the least significant byte, with a positive value.
Some compilers give a warning when using a char
(signed or not) type as an array index. The conversion to int
shuts the compiler up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With