<ctype.h>
Character Classification and ConversionFunction | Description |
---|---|
isalnum() |
Tests if a character is alphabetic or is a digit |
isalpha() |
Returns true if a character is alphabetic |
isblank() |
Tests if a character is word-separating whitespace |
iscntrl() |
Test if a character is a control character |
isdigit() |
Tests if a character is a digit |
isgraph() |
Tests if the character is printable and not a space |
islower() |
Tests if a character is lowercase |
isprint() |
Tests if a character is printable |
ispunct() |
Test if a character is punctuation |
isspace() |
Test if a character is whitespace |
isupper() |
Tests if a character is uppercase |
isxdigit() |
Tests if a character is a hexadecimal digit |
tolower() |
Convert a letter to lowercase |
toupper() |
Convert a letter to uppercase |
This collection of macros is good for testing characters to see if they’re of a certain class, such as alphabetic, numeric, control characters, etc.
Surprisingly, they take int
arguments instead of some kind of char
. This is so you can feed EOF
in for convenience if you have an integer representation of that. If not EOF
, the value passed in has to be representable in an unsigned char
. Otherwise it’s (dun dun DUUNNNN) undefined behavior. So you can forget about passing in your UTF-8 multibyte characters.
You can portably avoid this undefined behavior by casting the arguments to these functions to (unsigned char)
. This is irksome and ugly, admittedly. The values in the basic character set are all safe to use since they’re positive values that fit into an unsigned char
.
Also, the behavior of these functions varies based on locale.
In many of the pages in this section, I give some examples. These are from the “C” locale, and might vary if you’ve set a different locale.
Note that wide characters have their own set of classification functions, so don’t try to use these on wchar_t
s. Or else!
isalnum()
Tests if a character is alphabetic or is a digit
#include <ctype.h>
int isalnum(int c);
Tests if a character is alphabetic (A
-Z
or a
-z
) or a digit (0
-9
).
Is equivalent to:
(c) || isdigit(c) isalpha
Returns true if a character is alphabetic (A
-Z
or a
-z
) or a digit (0
-9
).
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", isalnum('a')? "yes": "no"); // yes
printf("%s\n", isalnum('B')? "yes": "no"); // yes
printf("%s\n", isalnum('5')? "yes": "no"); // yes
printf("%s\n", isalnum('?')? "yes": "no"); // no
}
isalpha()
Returns true if a character is alphabetic
#include <ctype.h>
int isalpha(int c);
Returns true for alphabetic characters (A
-Z
or a
-z
).
Technically (and in the “C” locale) equivalent to:
(c) || islower(c) isupper
Extra super technically, because I know you’re dying for this to be extra unnecessarily complex, it can also include some locale-specific characters for which this is true:
!iscntrl(c) && !isdigit(c) && !ispunct(c) && !isspace(c)
and this is true:
(c) || islower(c) isupper
Returns true for alphabetic characters (A
-Z
or a
-z
).
Or for any of the other crazy stuff in the description, above.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", isalpha('a')? "yes": "no"); // yes
printf("%s\n", isalpha('B')? "yes": "no"); // yes
printf("%s\n", isalpha('5')? "yes": "no"); // no
printf("%s\n", isalpha('?')? "yes": "no"); // no
}
isblank()
Tests if a character is word-separating whitespace
#include <ctype.h>
int isblank(int c);
True if the character is a whitespace character used to separate words in a single line.
For example, space (' '
) or horizontal tab ('\t'
). Other locales might define other blank characters.
Returns true if the character is a whitespace character used to separate words in a single line.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", isblank(' ')? "yes": "no"); // yes
printf("%s\n", isblank('\t')? "yes": "no"); // yes
printf("%s\n", isblank('\n')? "yes": "no"); // no
printf("%s\n", isblank('a')? "yes": "no"); // no
printf("%s\n", isblank('?')? "yes": "no"); // no
}
iscntrl()
Test if a character is a control character
#include <ctype.h>
int iscntrl(int c);
A control character is a locale-specific non-printing character.
For the “C” locale, this means control characters are in the range 0x00 to 0x1F (the character right before SPACE) and 0x7F (the DEL character).
Basically if it’s not an ASCII (or Unicode less than 128) printable character, it’s a control character in the “C” locale.
Returns true if c
is a control character.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", iscntrl('\t')? "yes": "no"); // yes (tab)
printf("%s\n", iscntrl('\n')? "yes": "no"); // yes (newline)
printf("%s\n", iscntrl('\r')? "yes": "no"); // yes (return)
printf("%s\n", iscntrl('\a')? "yes": "no"); // yes (bell)
printf("%s\n", iscntrl(' ')? "yes": "no"); // no
printf("%s\n", iscntrl('a')? "yes": "no"); // no
printf("%s\n", iscntrl('?')? "yes": "no"); // no
}
isdigit()
Tests if a character is a digit
#include <ctype.h>
int isdigit(int c);
Tests if c
is a digit in the range 0
-9
.
Returns true if the character is a digit, unsurprisingly.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", isdigit('0')? "yes": "no"); // yes
printf("%s\n", isdigit('5')? "yes": "no"); // yes
printf("%s\n", isdigit('a')? "yes": "no"); // no
printf("%s\n", isdigit('B')? "yes": "no"); // no
printf("%s\n", isdigit('?')? "yes": "no"); // no
}
isgraph()
Tests if the character is printable and not a space
#include <ctype.h>
int isgraph(int c);
Tests if c
is any printable character that isn’t a space (' '
).
Returns true if c
is any printable character that isn’t a space (' '
).
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", isgraph('0')? "yes": "no"); // yes
printf("%s\n", isgraph('a')? "yes": "no"); // yes
printf("%s\n", isgraph('B')? "yes": "no"); // yes
printf("%s\n", isgraph('?')? "yes": "no"); // yes
printf("%s\n", isgraph(' ')? "yes": "no"); // no
printf("%s\n", isgraph('\n')? "yes": "no"); // no
}
islower()
Tests if a character is lowercase
#include <ctype.h>
int islower(int c);
Tests if a character is lowercase, in the range a
-z
.
In other locales, there could be other lowercase characters. In all cases, to be lowercase, the following must be true:
!iscntrl(c) && !isdigit(c) && !ispunct(c) && !isspace(c)
Returns true if the character is lowercase.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", islower('c')? "yes": "no"); // yes
printf("%s\n", islower('0')? "yes": "no"); // no
printf("%s\n", islower('B')? "yes": "no"); // no
printf("%s\n", islower('?')? "yes": "no"); // no
printf("%s\n", islower(' ')? "yes": "no"); // no
}
isupper()
, isalpha()
, toupper()
, tolower()
isprint()
Tests if a character is printable
#include <ctype.h>
int isprint(int c);
Tests if a character is printable, including space (' '
). So like isgraph()
, except space isn’t left out in the cold.
Returns true if the character is printable, including space (' '
).
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", isprint('c')? "yes": "no"); // yes
printf("%s\n", isprint('0')? "yes": "no"); // yes
printf("%s\n", isprint(' ')? "yes": "no"); // yes
printf("%s\n", isprint('\r')? "yes": "no"); // no
}
ispunct()
Test if a character is punctuation
#include <ctype.h>
int ispunct(int c);
Tests if a character is punctuation.
In the “C” locale, this means:
!isspace(c) && !isalnum(c)
In other locales, there could be other punctuation characters (but they also can’t be space or alphanumeric).
True if the character is punctuation.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", ispunct(',')? "yes": "no"); // yes
printf("%s\n", ispunct('!')? "yes": "no"); // yes
printf("%s\n", ispunct('c')? "yes": "no"); // no
printf("%s\n", ispunct('0')? "yes": "no"); // no
printf("%s\n", ispunct(' ')? "yes": "no"); // no
printf("%s\n", ispunct('\n')? "yes": "no"); // no
}
isspace()
Test if a character is whitespace
#include <ctype.h>
int isspace(int c);
Tests if c
is a whitespace character. These are:
' '
)'\f'
)'\n'
)'\r'
)'\t'
)'\v'
)Other locales might specify other whitespace characters. isalnum()
is false for all whitespace characters.
True if the character is whitespace.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", isspace(' ')? "yes": "no"); // yes
printf("%s\n", isspace('\n')? "yes": "no"); // yes
printf("%s\n", isspace('\t')? "yes": "no"); // yes
printf("%s\n", isspace(',')? "yes": "no"); // no
printf("%s\n", isspace('!')? "yes": "no"); // no
printf("%s\n", isspace('c')? "yes": "no"); // no
}
isupper()
Tests if a character is uppercase
#include <ctype.h>
int isupper(int c);
Tests if a character is uppercase, in the range A
-Z
.
In other locales, there could be other uppercase characters. In all cases, to be uppercase, the following must be true:
!iscntrl(c) && !isdigit(c) && !ispunct(c) && !isspace(c)
Returns true if the character is uppercase.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", isupper('B')? "yes": "no"); // yes
printf("%s\n", isupper('c')? "yes": "no"); // no
printf("%s\n", isupper('0')? "yes": "no"); // no
printf("%s\n", isupper('?')? "yes": "no"); // no
printf("%s\n", isupper(' ')? "yes": "no"); // no
}
islower()
, isalpha()
, toupper()
, tolower()
isxdigit()
Tests if a character is a hexadecimal digit
#include <ctype.h>
int isxdigit(int c);
Returns true if the character is a hexadecimal digit. Namely if it’s 0
-9
, a
-f
, or A
-F
.
True if the character is a hexadecimal digit.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// testing this char
// v
printf("%s\n", isxdigit('B')? "yes": "no"); // yes
printf("%s\n", isxdigit('c')? "yes": "no"); // yes
printf("%s\n", isxdigit('2')? "yes": "no"); // yes
printf("%s\n", isxdigit('G')? "yes": "no"); // no
printf("%s\n", isxdigit('?')? "yes": "no"); // no
}
tolower()
Convert a letter to lowercase
#include <ctype.h>
int tolower(int c);
If the character is uppercase (i.e. isupper(c)
is true), this function returns the corresponding lowercase letter.
Different locales might have different upper- and lowercase letters.
Returns the lowercase value for an uppercase letter. If the letter isn’t uppercase, returns it unchanged.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// changing this char
// v
printf("%c\n", tolower('B')); // b (made lowercase!)
printf("%c\n", tolower('e')); // e (unchanged)
printf("%c\n", tolower('!')); // ! (unchanged)
}
toupper()
, islower()
, isupper()
toupper()
Convert a letter to uppercase
#include <ctype.h>
int toupper(int c);
If the character is lower (i.e. islower(c)
is true), this function returns the corresponding uppercase letter.
Different locales might have different upper- and lowercase letters.
Returns the uppercase value for a lowercase letter. If the letter isn’t lowercase, returns it unchanged.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
// changing this char
// v
printf("%c\n", toupper('B')); // B (unchanged)
printf("%c\n", toupper('e')); // E (made uppercase!)
printf("%c\n", toupper('!')); // ! (unchanged)
}
tolower()
, islower()
, isupper()