views:

29

answers:

1

What is the proper way to deal with character values which when casted to an unsigned char fall between {INT_MAX + 1 ... UCHAR_MAX} where UCHAR_MAX is greater than INT_MAX.

int is_digit(char c) {
    unsigned char uchar = c;
    if(uchar > INT_MAX)
        return MAYBE;
    return isdigit((int)uchar) ? YES : NO;
}
A: 

The Unicode character set (which is the largest commonly used) has character codes from 0 to 0x10ffff. So, the only possiblity for a character code to be larger than INT_MAX is if int is a 16 bit type (or specifically less than 22 bits). If that would be the case, then you simply can not store a character code in an int.

If int is a 32 bit type (or at least 22 bits) then a character code will not overflow when cast to an int.

Guffa
You can always use utf-8 and or arrays of char to store single code-points.Regardless of that fact. String literals are composed of characters and the ctype.h functions do not work on characters. The people that wrote C99 must have thought about this corner case.
Elite Mx
@Elite: Is it a corner case? Do you know of any system that uses full 21 bit Unicode code points (i.e. stores strings in UTF-32) and has a 16 bit int?
Guffa
Unicode has nothing to do with anything. Focus on these two facts. You have a character and it has a value (ZOMG! it could even be negative). Functions like isdigit may not accept your value. In closing - the functions in ctype.h are not reliable.
Elite Mx
@Elite: Well, you seem to have your opinion set already, so why ask a question?
Guffa
Once again completely irrelevant to absolutely anything. Good job buddy. You know what else? I found the correct answer on another website.
Elite Mx