jamesdlin jamesdlin - 2 months ago 7
C Question

How are wctype.h functions supposed to be used correctly?

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.

Answer

Aha. Upon re-reading the ISO C99 specification regarding wctype.h, it states:

For all functions described in this subclause that accept an argument of type wint_t, the value shall be representable as a wchar_t or shall equal the value of the macro WEOF. If this argument has any other value, the behavior is undefined. (§7.25.1, bullet 5)

Contrast this with the corresponding note for ctype.h:

In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined. (§7.4, bullet 1)

(emphasis mine)

So the wctype.h functions don't have any of the unsigned nonsense, and I should be able to pass wchar_t values to them directly.