Next: , Up: Character Handling   [Contents][Index]


4.1 Classification of Characters

This section explains the library functions for classifying characters. For example, isalpha is the function to test for an alphabetic character. It takes one argument, the character to test as an unsigned char value, and returns a nonzero integer if the character is alphabetic, and zero otherwise. You would use it like this:

if (isalpha ((unsigned char) c))
  printf ("The character `%c' is alphabetic.\n", c);

Each of the functions in this section tests for membership in a particular class of characters; each has a name starting with ‘is’. Each of them takes one argument, which is a character to test. The character argument must be in the value range of unsigned char (0 to 255 for the GNU C Library). On a machine where the char type is signed, it may be necessary to cast the argument to unsigned char, or mask it with ‘& 0xff’. (On unsigned char machines, this step is harmless, so portable code should always perform it.) The ‘is’ functions return an int which is treated as a boolean value.

All ‘is’ functions accept the special value EOF and return zero. (Note that EOF must not be cast to unsigned char for this to work.)

As an extension, the GNU C Library accepts signed char values as ‘is’ functions arguments in the range -128 to -2, and returns the result for the corresponding unsigned character. However, as there might be an actual character corresponding to the EOF integer constant, doing so may introduce bugs, and it is recommended to apply the conversion to the unsigned character range as appropriate.

The attributes of any given character can vary between locales. See Locales and Internationalization, for more information on locales.

These functions are declared in the header file ctype.h.

Function: int islower (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is a lower-case letter. The letter need not be from the Latin alphabet, any alphabet representable is valid.

Function: int isupper (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is an upper-case letter. The letter need not be from the Latin alphabet, any alphabet representable is valid.

Function: int isalpha (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is an alphabetic character (a letter). If islower or isupper is true of a character, then isalpha is also true.

In some locales, there may be additional characters for which isalpha is true—letters which are neither upper case nor lower case. But in the standard "C" locale, there are no such additional characters.

Function: int isdigit (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is a decimal digit (‘0’ through ‘9’).

Function: int isalnum (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is an alphanumeric character (a letter or number); in other words, if either isalpha or isdigit is true of a character, then isalnum is also true.

Function: int isxdigit (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is a hexadecimal digit. Hexadecimal digits include the normal decimal digits ‘0’ through ‘9’ and the letters ‘A’ through ‘F’ and ‘a’ through ‘f’.

Function: int ispunct (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is a punctuation character. This means any printing character that is not alphanumeric or a space character.

Function: int isspace (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is a whitespace character. In the standard "C" locale, isspace returns true for only the standard whitespace characters:

' '

space

'\f'

formfeed

'\n'

newline

'\r'

carriage return

'\t'

horizontal tab

'\v'

vertical tab

Function: int isblank (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is a blank character; that is, a space or a tab. This function was originally a GNU extension, but was added in ISO C99.

Function: int isgraph (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is a graphic character; that is, a character that has a glyph associated with it. The whitespace characters are not considered graphic.

Function: int isprint (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is a printing character. Printing characters include all the graphic characters, plus the space (‘ ’) character.

Function: int iscntrl (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is a control character (that is, a character that is not a printing character).

Function: int isascii (int c)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

Returns true if c is a 7-bit unsigned char value that fits into the US/UK ASCII character set. This function is a BSD extension and is also an SVID extension.


Next: Case Conversion, Up: Character Handling   [Contents][Index]