Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

This Compliance with this rule is complicated by the fact that the char data type might, in any implementation, be signed or unsigned.

...

This non-compliant code example may pass illegal values to the ctype functions isspace() function.

Code Block
bgColor#FFcccc
size_t count_whitespace(const char *s) {
  const char *t = s;
  while (isspace(*t))  /* possibly *t < 0 */
    ++t;
  return t - s;
}

Compliant Solution

...

(Unsigned Char)

Pass character strings around explicitly using unsigned characters.

...

Wiki Markup
This approach is inconvenient when you need to interwork with other functions that haven't been designed with this approach in mind, such as the string handling functions found in the standard library \[[Kettlewell 02|AA. C References#Kettlewell 02]\].

Compliant Solution

...

(Cast)

This compliant solution uses an explicit a cast.

Code Block
bgColor#ccccff
size_t count_whitespace(const char *s) {
  const char *t = s;
  while (isspace((unsigned char)*t))
    ++t;
  return t - s;
}

...