Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Referenced Appendix J, POSIX/XSI, and STR34-C. Simplified examples and removed redundant unnecessary const-qualification from compliant solution.

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 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.

...

isalnum()

isalpha()

isascii() XSI

isblank()

iscntrl()

isdigit()

isgraph()

islower()

isprint()

ispunct()

isspace()

isupper()

isxdigit()

toascii() XSI

toupper()

tolower()

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
bgColor#FFcccc
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
bgColor#ccccff
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;
}

...