Chapter Contents

Previous

Next
Function Categories

Character Type Macros and Functions

The character type header files, <ctype.h> and <lctype.h> , define several macros that are useful in the analysis of text data. Most of these macros enable you to determine quickly the type of a character (whether it is alphabetic, numeric, punctuation, and so on). These macros refer to an external array that is indexed by the character itself, so they are generally much faster than functions that check the character against a range or discrete list of values. Note that this array is actually indexed by the character value plus 1, so the standard EOF value (-1) can be tested in a macro without yielding a nonsense result. EOF yields a 0 result for all of the macros because it is not defined as any of the character types. Also, note that the results produced by most of these functions are affected by the current locale's LC_CTYPE category, which may cause a different character type array to be used than the one supplied for the default C locale. See Chapter 10, "Localization," in SAS/C Library Reference, Volume 2 for details on locales.

Another advantage of the character type macros is that they prevent problems when programs are moved between machines that use ASCII versus EBCDIC character sets. Programs using these macros are not dependent on a specific character set.

The following are character type macros and functions:
isalnum alphanumeric character test
isalpha alphabetic character test
isascii ASCII character test
iscntrl control character test
iscsym test for valid C identifier symbol
iscsymf test for valid C identifier initial symbol
isdigit test for numeric character
isebcdic EBCDIC character test
isgraph graphic character test
islower lowercase alphabetic character test
isprint printing character test
ispunct punctuation test
isspace white space test
isupper uppercase alphabetic character test
isxdigit hexadecimal digit test
toebcdic reduce integer to EBCDIC character
tolower translate uppercase character to lowercase
toupper translate lowercase character to uppercase.

Character Type Macros and Functions and Their Return Values lists the macros and functions defined in the character type header files <ctype.h> and <lctype.h> . The library conforms to the ISO/ANSI specification in that the macro arguments are evaluated only once. However, many implementations do not conform to this specification. For maximum portability, beware of the side effects of using expressions such as function calls and increment or decrement operators. You should include <ctype.h> or <lctype.h> if you use any of these macros; otherwise, the compiler generates a reference to a function of the same name.

Character Type Macros and Functions and Their Return Values
Function Return Value
isalnum(c) nonzero if c is alphabetic or digit; 0 if not
isalpha(c) nonzero if c is alphabetic; 0 if not
isascii(c)* nonzero if c is the EBCDIC equivalent of an ASCII character; 0 if not
iscntrl(c) nonzero if c is control character; 0 if not
iscsym(c)* nonzero if valid character for C identifier; 0 if not
iscsymf(c) nonzero if valid first character for C identifier; 0 if not
isdigit(c) * nonzero if c is a digit 0-9; 0 if not
isebcdic(c) * nonzero if a valid EBCDIC character; 0 if not
isgraph(c)
nonzero if c is graphic (excluding the blank character); 0 if not
islower(c) nonzero if c is lowercase; 0 if not
isprint(c) nonzero if c is printable (including blank); 0 if not
ispunct(c) nonzero if c is punctuation; 0 if not
isspace(c) nonzero if c is white space; 0 if not
isupper(c) nonzero if c is uppercase; 0 if not
isxdigit(c) * nonzero if c is a hexadecimal digit (0-9, A-F, a-f); if not
toebcdic(c) * truncates integer to valid EBCDIC character
tolower(c) converts c to lowercase, if uppercase
toupper(c) converts c to uppercase, if lowercase

In Character Type Macros and Functions and Their Return Values, functions marked with *are not affected by the locale's LC_TYPE category.

Note:    The toupper and tolower macros generate the value of c unchanged if it does not qualify for the conversion.  [cautionend]


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.