According to Section 7.4 of C99 ,(see also bullet 107 of Appendix J):
The header
<ctype.h>
declares several functions useful for classifying and mapping characters. In all cases the argument is anint
, the value of which shall be representable as anunsigned char
or shall equal the value of the macroEOF
. If the argument has any other value, the behavior is undefined.
...
| | | |
| | | |
| | | |
| | | |
Wiki Markup |
---|
Note: XSI denotes an X/Open System Interfaces Extension to \[ISO/IEC 9945\] -- POSIX ^®^. The functions are not defined by C99. |
A generalization of this rule is STR34-C. Cast characters to unsigned types before converting to larger integer sizes.
Noncompliant Code Example
...
Code Block | ||
---|---|---|
| ||
size_t count_preceding_whitespace(const char *s) { const char *t = s; size_t length = strlen(s) + 1; /* possibly *t < 0 */ while (isspace(*t) && isspace(t - s < length*t)) { ++t; } return t - s; } |
...
Code Block | ||
---|---|---|
| ||
size_t count_preceding_whitespace(const char *s) { const char *t = s; size_t length = strlen(s) + 1; while (while (*t && isspace((const unsigned char)*t) && (t - s < length)) { ++t; } return t - s; } |
...