]> sourceware.org Git - glibc.git/blame - manual/ctype.texi
Update.
[glibc.git] / manual / ctype.texi
CommitLineData
28f540f4 1@node Character Handling, String and Array Utilities, Memory Allocation, Top
7a68c94a 2@c %MENU% Character testing and conversion functions
28f540f4
RM
3@chapter Character Handling
4
5Programs that work with characters and strings often need to classify a
6character---is it alphabetic, is it a digit, is it whitespace, and so
7on---and perform case conversion operations on characters. The
8functions in the header file @file{ctype.h} are provided for this
9purpose.
10@pindex ctype.h
11
12Since the choice of locale and character set can alter the
13classifications of particular character codes, all of these functions
14are affected by the current locale. (More precisely, they are affected
15by the locale currently selected for character classification---the
16@code{LC_CTYPE} category; see @ref{Locale Categories}.)
17
18@menu
19* Classification of Characters:: Testing whether characters are
20 letters, digits, punctuation, etc.
21
22* Case Conversion:: Case mapping, and the like.
23@end menu
24
25@node Classification of Characters, Case Conversion, , Character Handling
26@section Classification of Characters
27@cindex character testing
28@cindex classification of characters
29@cindex predicates on characters
30@cindex character predicates
31
32This section explains the library functions for classifying characters.
33For example, @code{isalpha} is the function to test for an alphabetic
34character. It takes one argument, the character to test, and returns a
35nonzero integer if the character is alphabetic, and zero otherwise. You
36would use it like this:
37
38@smallexample
39if (isalpha (c))
40 printf ("The character `%c' is alphabetic.\n", c);
41@end smallexample
42
43Each of the functions in this section tests for membership in a
44particular class of characters; each has a name starting with @samp{is}.
45Each of them takes one argument, which is a character to test, and
46returns an @code{int} which is treated as a boolean value. The
47character argument is passed as an @code{int}, and it may be the
f65fd747 48constant value @code{EOF} instead of a real character.
28f540f4
RM
49
50The attributes of any given character can vary between locales.
51@xref{Locales}, for more information on locales.@refill
52
53These functions are declared in the header file @file{ctype.h}.
54@pindex ctype.h
55
56@cindex lower-case character
57@comment ctype.h
f65fd747 58@comment ISO
28f540f4
RM
59@deftypefun int islower (int @var{c})
60Returns true if @var{c} is a lower-case letter.
61@end deftypefun
62
63@cindex upper-case character
64@comment ctype.h
f65fd747 65@comment ISO
28f540f4
RM
66@deftypefun int isupper (int @var{c})
67Returns true if @var{c} is an upper-case letter.
68@end deftypefun
69
70@cindex alphabetic character
71@comment ctype.h
f65fd747 72@comment ISO
28f540f4
RM
73@deftypefun int isalpha (int @var{c})
74Returns true if @var{c} is an alphabetic character (a letter). If
75@code{islower} or @code{isupper} is true of a character, then
76@code{isalpha} is also true.
77
78In some locales, there may be additional characters for which
cc3fa755 79@code{isalpha} is true---letters which are neither upper case nor lower
28f540f4
RM
80case. But in the standard @code{"C"} locale, there are no such
81additional characters.
82@end deftypefun
83
84@cindex digit character
85@cindex decimal digit character
86@comment ctype.h
f65fd747 87@comment ISO
28f540f4
RM
88@deftypefun int isdigit (int @var{c})
89Returns true if @var{c} is a decimal digit (@samp{0} through @samp{9}).
90@end deftypefun
91
92@cindex alphanumeric character
93@comment ctype.h
f65fd747 94@comment ISO
28f540f4
RM
95@deftypefun int isalnum (int @var{c})
96Returns true if @var{c} is an alphanumeric character (a letter or
97number); in other words, if either @code{isalpha} or @code{isdigit} is
98true of a character, then @code{isalnum} is also true.
99@end deftypefun
100
101@cindex hexadecimal digit character
102@comment ctype.h
f65fd747 103@comment ISO
28f540f4
RM
104@deftypefun int isxdigit (int @var{c})
105Returns true if @var{c} is a hexadecimal digit.
106Hexadecimal digits include the normal decimal digits @samp{0} through
107@samp{9} and the letters @samp{A} through @samp{F} and
108@samp{a} through @samp{f}.
109@end deftypefun
110
111@cindex punctuation character
112@comment ctype.h
f65fd747 113@comment ISO
28f540f4
RM
114@deftypefun int ispunct (int @var{c})
115Returns true if @var{c} is a punctuation character.
116This means any printing character that is not alphanumeric or a space
117character.
118@end deftypefun
119
120@cindex whitespace character
121@comment ctype.h
f65fd747 122@comment ISO
28f540f4
RM
123@deftypefun int isspace (int @var{c})
124Returns true if @var{c} is a @dfn{whitespace} character. In the standard
125@code{"C"} locale, @code{isspace} returns true for only the standard
126whitespace characters:
127
128@table @code
129@item ' '
130space
131
132@item '\f'
133formfeed
134
135@item '\n'
136newline
137
138@item '\r'
139carriage return
140
141@item '\t'
142horizontal tab
143
144@item '\v'
145vertical tab
146@end table
147@end deftypefun
148
149@cindex blank character
150@comment ctype.h
151@comment GNU
152@deftypefun int isblank (int @var{c})
153Returns true if @var{c} is a blank character; that is, a space or a tab.
154This function is a GNU extension.
155@end deftypefun
156
157@cindex graphic character
158@comment ctype.h
f65fd747 159@comment ISO
28f540f4
RM
160@deftypefun int isgraph (int @var{c})
161Returns true if @var{c} is a graphic character; that is, a character
162that has a glyph associated with it. The whitespace characters are not
163considered graphic.
164@end deftypefun
165
166@cindex printing character
167@comment ctype.h
f65fd747 168@comment ISO
28f540f4
RM
169@deftypefun int isprint (int @var{c})
170Returns true if @var{c} is a printing character. Printing characters
171include all the graphic characters, plus the space (@samp{ }) character.
172@end deftypefun
173
174@cindex control character
175@comment ctype.h
f65fd747 176@comment ISO
28f540f4
RM
177@deftypefun int iscntrl (int @var{c})
178Returns true if @var{c} is a control character (that is, a character that
179is not a printing character).
180@end deftypefun
181
182@cindex ASCII character
183@comment ctype.h
184@comment SVID, BSD
185@deftypefun int isascii (int @var{c})
186Returns true if @var{c} is a 7-bit @code{unsigned char} value that fits
187into the US/UK ASCII character set. This function is a BSD extension
188and is also an SVID extension.
189@end deftypefun
190
191@node Case Conversion, , Classification of Characters, Character Handling
192@section Case Conversion
193@cindex character case conversion
194@cindex case conversion of characters
195@cindex converting case of characters
196
197This section explains the library functions for performing conversions
198such as case mappings on characters. For example, @code{toupper}
199converts any character to upper case if possible. If the character
200can't be converted, @code{toupper} returns it unchanged.
201
202These functions take one argument of type @code{int}, which is the
203character to convert, and return the converted character as an
204@code{int}. If the conversion is not applicable to the argument given,
205the argument is returned unchanged.
206
f65fd747 207@strong{Compatibility Note:} In pre-@w{ISO C} dialects, instead of
28f540f4
RM
208returning the argument unchanged, these functions may fail when the
209argument is not suitable for the conversion. Thus for portability, you
210may need to write @code{islower(c) ? toupper(c) : c} rather than just
211@code{toupper(c)}.
212
213These functions are declared in the header file @file{ctype.h}.
214@pindex ctype.h
215
216@comment ctype.h
f65fd747 217@comment ISO
28f540f4
RM
218@deftypefun int tolower (int @var{c})
219If @var{c} is an upper-case letter, @code{tolower} returns the corresponding
220lower-case letter. If @var{c} is not an upper-case letter,
221@var{c} is returned unchanged.
222@end deftypefun
223
224@comment ctype.h
f65fd747 225@comment ISO
28f540f4
RM
226@deftypefun int toupper (int @var{c})
227If @var{c} is a lower-case letter, @code{tolower} returns the corresponding
228upper-case letter. Otherwise @var{c} is returned unchanged.
229@end deftypefun
230
231@comment ctype.h
232@comment SVID, BSD
233@deftypefun int toascii (int @var{c})
234This function converts @var{c} to a 7-bit @code{unsigned char} value
235that fits into the US/UK ASCII character set, by clearing the high-order
236bits. This function is a BSD extension and is also an SVID extension.
237@end deftypefun
238
239@comment ctype.h
240@comment SVID
241@deftypefun int _tolower (int @var{c})
242This is identical to @code{tolower}, and is provided for compatibility
243with the SVID. @xref{SVID}.@refill
244@end deftypefun
245
246@comment ctype.h
247@comment SVID
248@deftypefun int _toupper (int @var{c})
249This is identical to @code{toupper}, and is provided for compatibility
250with the SVID.
251@end deftypefun
This page took 0.095511 seconds and 5 git commands to generate.