1 @node Character Handling, String and Array Utilities, Memory Allocation, Top
2 @chapter Character Handling
4 Programs that work with characters and strings often need to classify a
5 character---is it alphabetic, is it a digit, is it whitespace, and so
6 on---and perform case conversion operations on characters. The
7 functions in the header file @file{ctype.h} are provided for this
11 Since the choice of locale and character set can alter the
12 classifications of particular character codes, all of these functions
13 are affected by the current locale. (More precisely, they are affected
14 by the locale currently selected for character classification---the
15 @code{LC_CTYPE} category; see @ref{Locale Categories}.)
18 * Classification of Characters:: Testing whether characters are
19 letters, digits, punctuation, etc.
21 * Case Conversion:: Case mapping, and the like.
24 @node Classification of Characters, Case Conversion, , Character Handling
25 @section Classification of Characters
26 @cindex character testing
27 @cindex classification of characters
28 @cindex predicates on characters
29 @cindex character predicates
31 This section explains the library functions for classifying characters.
32 For example, @code{isalpha} is the function to test for an alphabetic
33 character. It takes one argument, the character to test, and returns a
34 nonzero integer if the character is alphabetic, and zero otherwise. You
35 would use it like this:
39 printf ("The character `%c' is alphabetic.\n", c);
42 Each of the functions in this section tests for membership in a
43 particular class of characters; each has a name starting with @samp{is}.
44 Each of them takes one argument, which is a character to test, and
45 returns an @code{int} which is treated as a boolean value. The
46 character argument is passed as an @code{int}, and it may be the
47 constant value @code{EOF} instead of a real character.
49 The attributes of any given character can vary between locales.
50 @xref{Locales}, for more information on locales.@refill
52 These functions are declared in the header file @file{ctype.h}.
55 @cindex lower-case character
58 @deftypefun int islower (int @var{c})
59 Returns true if @var{c} is a lower-case letter.
62 @cindex upper-case character
65 @deftypefun int isupper (int @var{c})
66 Returns true if @var{c} is an upper-case letter.
69 @cindex alphabetic character
72 @deftypefun int isalpha (int @var{c})
73 Returns true if @var{c} is an alphabetic character (a letter). If
74 @code{islower} or @code{isupper} is true of a character, then
75 @code{isalpha} is also true.
77 In some locales, there may be additional characters for which
78 @code{isalpha} is true--letters which are neither upper case nor lower
79 case. But in the standard @code{"C"} locale, there are no such
80 additional characters.
83 @cindex digit character
84 @cindex decimal digit character
87 @deftypefun int isdigit (int @var{c})
88 Returns true if @var{c} is a decimal digit (@samp{0} through @samp{9}).
91 @cindex alphanumeric character
94 @deftypefun int isalnum (int @var{c})
95 Returns true if @var{c} is an alphanumeric character (a letter or
96 number); in other words, if either @code{isalpha} or @code{isdigit} is
97 true of a character, then @code{isalnum} is also true.
100 @cindex hexadecimal digit character
103 @deftypefun int isxdigit (int @var{c})
104 Returns true if @var{c} is a hexadecimal digit.
105 Hexadecimal digits include the normal decimal digits @samp{0} through
106 @samp{9} and the letters @samp{A} through @samp{F} and
107 @samp{a} through @samp{f}.
110 @cindex punctuation character
113 @deftypefun int ispunct (int @var{c})
114 Returns true if @var{c} is a punctuation character.
115 This means any printing character that is not alphanumeric or a space
119 @cindex whitespace character
122 @deftypefun int isspace (int @var{c})
123 Returns true if @var{c} is a @dfn{whitespace} character. In the standard
124 @code{"C"} locale, @code{isspace} returns true for only the standard
125 whitespace characters:
148 @cindex blank character
151 @deftypefun int isblank (int @var{c})
152 Returns true if @var{c} is a blank character; that is, a space or a tab.
153 This function is a GNU extension.
156 @cindex graphic character
159 @deftypefun int isgraph (int @var{c})
160 Returns true if @var{c} is a graphic character; that is, a character
161 that has a glyph associated with it. The whitespace characters are not
165 @cindex printing character
168 @deftypefun int isprint (int @var{c})
169 Returns true if @var{c} is a printing character. Printing characters
170 include all the graphic characters, plus the space (@samp{ }) character.
173 @cindex control character
176 @deftypefun int iscntrl (int @var{c})
177 Returns true if @var{c} is a control character (that is, a character that
178 is not a printing character).
181 @cindex ASCII character
184 @deftypefun int isascii (int @var{c})
185 Returns true if @var{c} is a 7-bit @code{unsigned char} value that fits
186 into the US/UK ASCII character set. This function is a BSD extension
187 and is also an SVID extension.
190 @node Case Conversion, , Classification of Characters, Character Handling
191 @section Case Conversion
192 @cindex character case conversion
193 @cindex case conversion of characters
194 @cindex converting case of characters
196 This section explains the library functions for performing conversions
197 such as case mappings on characters. For example, @code{toupper}
198 converts any character to upper case if possible. If the character
199 can't be converted, @code{toupper} returns it unchanged.
201 These functions take one argument of type @code{int}, which is the
202 character to convert, and return the converted character as an
203 @code{int}. If the conversion is not applicable to the argument given,
204 the argument is returned unchanged.
206 @strong{Compatibility Note:} In pre-@w{ISO C} dialects, instead of
207 returning the argument unchanged, these functions may fail when the
208 argument is not suitable for the conversion. Thus for portability, you
209 may need to write @code{islower(c) ? toupper(c) : c} rather than just
212 These functions are declared in the header file @file{ctype.h}.
217 @deftypefun int tolower (int @var{c})
218 If @var{c} is an upper-case letter, @code{tolower} returns the corresponding
219 lower-case letter. If @var{c} is not an upper-case letter,
220 @var{c} is returned unchanged.
225 @deftypefun int toupper (int @var{c})
226 If @var{c} is a lower-case letter, @code{tolower} returns the corresponding
227 upper-case letter. Otherwise @var{c} is returned unchanged.
232 @deftypefun int toascii (int @var{c})
233 This function converts @var{c} to a 7-bit @code{unsigned char} value
234 that fits into the US/UK ASCII character set, by clearing the high-order
235 bits. This function is a BSD extension and is also an SVID extension.
240 @deftypefun int _tolower (int @var{c})
241 This is identical to @code{tolower}, and is provided for compatibility
242 with the SVID. @xref{SVID}.@refill
247 @deftypefun int _toupper (int @var{c})
248 This is identical to @code{toupper}, and is provided for compatibility