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

Various functions is...(e.g. isalpha, isdigit) in the ctype.hnot completely predictable. They accept arguments int, but expect signed values ​​in the range unsigned char, so on the platform where it is charsigned, passing the value chardirectly can lead to an undesirable sign extension. I believe a typical approach to handling this is to explicitly specify first unsigned char.

Ok, but what is the right, portable way to solve various isw...functions in wctype.h? wchar_t, like char, can also be signed or unsigned, but since wchar_tit is itself typedef, the file name unsigned wchar_tis illegal.

+5
source share
2 answers

Yeah. After re-reading the ISO C99 specification in relation wctype.h, it states:

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

Contrast this with the corresponding note for ctype.h:

In all cases, the argument is equal int, the value of which must be represented as or equal to the value of the macro . If the argument has any other value, the behavior is undefined. (§7.4, bullet 1) unsigned charEOF

(emphasis mine)

, wctype.h , wchar_t.

+1

wint_t? iswXxxxx() wint_t:

ISO 9899: 1999 , :

§7.25 <wctype.h>

§7.25.2.1.1 iswalnum

#include <wctype.h>
int iswalnum(wint_t wc);

iswalnum , iswalpha iswdigit .

§7.24 <wchar.h>

§7.24.1 :

wint_t

, , , , , , (. WEOF ); 269)

269)wchar_t wint_t .

" " , int, short unsigned short, sizeof(short) == sizeof(int) ( , 16- ).

§7.17 <stddef.h>

wchar_t

, , ; , .

, iswalnum() , wchar_t WEOF, . - , undefined.

+2

All Articles