The various is...
functions (e.g. isalpha
, isdigit
) in ctype.h
aren't entirely predictable. They take int
arguments but expect character values in the unsigned char
range, so on a platform where char
is signed, passing a char
value directly could lead to undesirable sign extension. I believe that the typical approach to handling this is to explicitly cast to an unsigned char
first.
Okay, but what is the proper, portable way to deal with the various isw...
functions in wctype.h
? wchar_t
, like char
, also may be signed or unsigned, but because wchar_t
is itself a typedef
, a typename of unsigned wchar_t
is illegal.
Isn't that what wint_t
is for? The iswXxxxx()
functions take a wint_t
type:
ISO 9899:1999 covers this in various sections, working backwards:
§7.25 Wide character classification and mapping utilities
<wctype.h>
§7.25.2.1.1 The iswalnum function
Synopsis
#include <wctype.h> int iswalnum(wint_t wc);
Description
The iswalnum function tests for any wide character for which iswalpha or iswdigit is true.
§7.24 Extended multibyte and wide character utilities
<wchar.h>
§7.24.1 Introduction:
wint_t
which is an integer type unchanged by default argument promotions that can hold any value corresponding to members of the extended character set, as well as at least one value that does not correspond to any member of the extended character set (see WEOF below);269)
269)
wchar_t
andwint_t
can be the same integer type.
The 'unchanged by default argument promotions' should mean that it has to be as big as an int
, though it could be a short
or unsigned short
if sizeof(short) == sizeof(int)
(which is seldom the case these days, though it was true for some 16-bit systems).
§7.17 Common definitions
<stddef.h>
wchar_t
which is an integer type whose range of values can represent distinct codes for all members of the largest extended character set specified among the supported locales; the null character shall have the code value zero and each member of the basic character set shall have a code value equal to its value when used as the lone character in an integer character constant.
As long as the value passed to iswalnum()
or its kin is a valid wchar_t
or WEOF, the function will work correctly. If you manufactured the value out of thin air and manage to get the value wrong, you get undefined behaviour.
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