Versions Compared

Key

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

...

  • The type of each element of a string literal.
  • Used for character data from a limited character set (where signedness has little meaning) as opposed to integer data.

wide characters wchar_t

  • Used for natural-language character data.

int

  • Used for data that could be either EOF (a negative value) or character data interpreted as unsigned char and then converted to int. As a result, returned by fgetc(), getc(), getchar(), and ungetc(). Also, accepted by the character handling functions from <ctype.h>, because they might be passed the result of fgetc(), etc.
  • The type of a character constant. Its value is that of a plain char converted to int.

Note that the two different ways a character is used as an int (as an unsigned char + EOF, or as a plain char, converted to int) can lead to confusion. For example, isspace('\200') results in undefined behavior when char is signed.

unsigned char

  • Used internally for string comparison functions, even though these operate on character data.  Therefore, the result of a string comparison does not depend on whether plain char is signed.
  • Used for situations where the object being manipulated might be of any type, and it is necessary to access all bits of that object, as with fwrite().

...

wchar_t

  • Wide characters are used for natural-language character data.

Risk Assessment

Understanding how to represent characters and character strings can eliminate many common programming errors that lead to software vulnerabilities.

...