Prev | Contents | Next

5 <ctype.h> Character Classification and Conversion

Function 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_ts. Or else!


5.1 isalnum()

Tests if a character is alphabetic or is a digit

Synopsis

#include <ctype.h>

int isalnum(int c);

Description

Tests if a character is alphabetic (A-Z or a-z) or a digit (0-9).

Is equivalent to:

isalpha(c) || isdigit(c)

Return Value

Returns true if a character is alphabetic (A-Z or a-z) or a digit (0-9).

Example

#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
}

See Also

isalpha(), isdigit()


5.2 isalpha()

Returns true if a character is alphabetic

Synopsis

#include <ctype.h>

int isalpha(int c);

Description

Returns true for alphabetic characters (A-Z or a-z).

Technically (and in the “C” locale) equivalent to:

isupper(c) || islower(c)

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:

isupper(c) || islower(c)

Return Value

Returns true for alphabetic characters (A-Z or a-z).

Or for any of the other crazy stuff in the description, above.

Example

#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
}

See Also

isalnum()


5.3 isblank()

Tests if a character is word-separating whitespace

Synopsis

#include <ctype.h>

int isblank(int c);

Description

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.

Return Value

Returns true if the character is a whitespace character used to separate words in a single line.

Example

#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
}

See Also

isspace()


5.4 iscntrl()

Test if a character is a control character

Synopsis

#include <ctype.h>

int iscntrl(int c);

Description

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.

Return Value

Returns true if c is a control character.

Example

#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
}

See Also

isgraph(), isprint()


5.5 isdigit()

Tests if a character is a digit

Synopsis

#include <ctype.h>

int isdigit(int c);

Description

Tests if c is a digit in the range 0-9.

Return Value

Returns true if the character is a digit, unsurprisingly.

Example

#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
}

See Also

isalnum(), isxdigit()


5.6 isgraph()

Tests if the character is printable and not a space

Synopsis

#include <ctype.h>

int isgraph(int c);

Description

Tests if c is any printable character that isn’t a space (' ').

Return Value

Returns true if c is any printable character that isn’t a space (' ').

Example

#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
}

See Also

iscntrl(), isprint()


5.7 islower()

Tests if a character is lowercase

Synopsis

#include <ctype.h>

int islower(int c);

Description

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)

Return Value

Returns true if the character is lowercase.

Example

#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
}

See Also

isupper(), isalpha(), toupper(), tolower()


5.8 isprint()

Tests if a character is printable

Synopsis

#include <ctype.h>

int isprint(int c);

Description

Tests if a character is printable, including space (' '). So like isgraph(), except space isn’t left out in the cold.

Return Value

Returns true if the character is printable, including space (' ').

Example

#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
}

See Also

isgraph(), iscntrl()


5.9 ispunct()

Test if a character is punctuation

Synopsis

#include <ctype.h>

int ispunct(int c);

Description

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).

Return Value

True if the character is punctuation.

Example

#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
}

See Also

isspace(), isalnum()


5.10 isspace()

Test if a character is whitespace

Synopsis

#include <ctype.h>

int isspace(int c);

Description

Tests if c is a whitespace character. These are:

Other locales might specify other whitespace characters. isalnum() is false for all whitespace characters.

Return Value

True if the character is whitespace.

Example

#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
}

See Also

isblank()


5.11 isupper()

Tests if a character is uppercase

Synopsis

#include <ctype.h>

int isupper(int c);

Description

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)

Return Value

Returns true if the character is uppercase.

Example

#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
}

See Also

islower(), isalpha(), toupper(), tolower()


5.12 isxdigit()

Tests if a character is a hexadecimal digit

Synopsis

#include <ctype.h>

int isxdigit(int c);

Description

Returns true if the character is a hexadecimal digit. Namely if it’s 0-9, a-f, or A-F.

Return Value

True if the character is a hexadecimal digit.

Example

#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
}

See Also

isdigit()


5.13 tolower()

Convert a letter to lowercase

Synopsis

#include <ctype.h>

int tolower(int c);

Description

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.

Return Value

Returns the lowercase value for an uppercase letter. If the letter isn’t uppercase, returns it unchanged.

Example

#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)
}

See Also

toupper(), islower(), isupper()


5.14 toupper()

Convert a letter to uppercase

Synopsis

#include <ctype.h>

int toupper(int c);

Description

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.

Return Value

Returns the uppercase value for a lowercase letter. If the letter isn’t lowercase, returns it unchanged.

Example

#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)
}

See Also

tolower(), islower(), isupper()


Prev | Contents | Next