You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

According to Section 7.4 of C99:

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.

The char data type might, in any implementation, be signed or unsigned.

Non-Compliant Code Example

This non-compliant code example may pass illegal values to the ctype functions.

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

Compliant Solution 2

This compliant solution uses an explicit cast.

size_t count_whitespace(const char *s) {
  const char *t = s;
  while(isspace((unsigned char)*t))
    ++t;
  return t - s;
}

References

  • No labels