Open In App

ctype.h(<cctype>) library in C/C++ with Examples

As string.h header file contains inbuilt functions to handle Strings in C/C++, the ctype.h/<cctype> contains inbuilt functions to handle characters in C/C++ respectively.

Characters are of two types:



  1. Printable Characters: The characters that are displayed on the terminal.
  2. Control Characters: The characters that are initiated to perform a specific operation.

The arguments passed to character functions should be of integer type. If we pass characters instead of an integer, the characters are typecasted into integers(corresponding ASCII values) and those integers are passed as arguments.

The below functions under ctype.h/<cctype> header file are applied on normal characters. Wide character functions are used for the characters of type wchar_t.



S.No Function Description Return Values
1. isalnum() This function identifies the alphanumeric characters Returns 0 if the passed argument is non – alphanumeric character
Returns non zero value if the passed argument is alphanumeric character
2. isalpha() This function identifies the alphabets from other characters Returns 0 if the passed argument is not an alphabet
Returns non zero value if the passed argument is an alphabet
3. isblank() This function identifies the blank spaces from other characters Returns 0 if the passed argument is not a blank space
Returns nonzero value if the passed argument is a blank space
4. iscntrl() This function identifies the control characters(\n, \b, \t, \r). Returns 0 if the passed argument is not a control character
Returns nonzero value if the passed argument is a control character
5. isdigit() This function identifies numbers in character. Returns 0 if the passed argument is not a number
Returns nonzero value if the passed argument is a number
6. islower() This function identifies the lowercase alphabets. Returns 0 if the passed argument is not a lowercase alphabet
Returns nonzero value if the passed argument is a lowercase alphabet
7. isprint() This function identifies the printable characters. Returns 0 if the passed argument is a non printable character
Returns nonzero value if the passed argument is a printable character
8. ispunct() This function identifies punctuation characters (characters that are neither alphanumeric nor space). Returns 0 if the passed argument is not a punctuation character
Returns nonzero value if the passed argument is a punctuation character
9. isspace() This function identifies white-space characters. Returns 0 if the passed argument is not a white-space character
Returns nonzero value if the passed argument is a white-space character
10. isupper() This function identifies the uppercase alphabets. Returns 0 if the passed argument is not an uppercase alphabet
Returns nonzero value if the passed argument is an uppercase alphabet
11. isxdigit() This function identifies the hexadecimal digit. Returns 0 if the passed argument is not a hexadecimal digit
Returns nonzero value if the passed argument is an hexadecimal digit
12. tolower() This function converts uppercase alphabet to lowercase alphabet. Returns lowercase alphabet of the corresponding uppercase alphabet
13. toupper() This function convert lowercase alphabet to uppercase alphabet. Returns uppercase alphabet of the corresponding lowercase alphabet

Below are examples to implement some of the above functions:


Article Tags :