C Reference ยท ctype.h, math.h and others
C isspace(), isupper() and islower() functions
Reference for C isspace(), isblank(), isupper() and islower() from ctype.h: which characters count as whitespace, case tests and trimming a string.
Four more classifiers from <ctype.h>: isspace() is true for whitespace, isblank() for the narrower "blank" set, isupper() for uppercase letters and islower() for lowercase letters. As with the other classification functions, they take an int that holds an unsigned char value (or EOF) and return zero for false, non-zero for true.
Whitespace tests are what you need to skip blanks between tokens or to trim a line. The case tests let you decide whether a letter needs conversion before you call toupper or tolower.
#include <ctype.h> int isspace(int c); int isblank(int c); int isupper(int c); int islower(int c);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int isspace(int c); int isblank(int c); int isupper(int c); int islower(int c);
| Name | Description |
|---|---|
c |
A value representable as unsigned char, or EOF. |
Returns: non-zero if the character is in the class, otherwise zero.
Errors / undefined behavior: any other value for c (for example a negative char) is undefined behavior.
Usage notes
isspaceset in the"C"locale: space, form feed\f, newline\n, carriage return\r, horizontal tab\tand vertical tab\v. Other locales may add more single-byte characters.isblankset in the"C"locale: only space and horizontal tab. Newline is whitespace but not blank, which is useful when you want to skip blanks inside a line but stop at its end.isblankarrived in C99.isupperandislowerare true only for letters of that case. Digits, punctuation and letters without case (in some locales) are false for both, so!isupper(c)does not mean "lowercase".- Cast
chartounsigned charbefore calling, as in the examples. A negative value other thanEOFis undefined behavior. - Trailing newline. Lines read with
fgetsend in\n; a trim like the one above removes it, together with a possible\rfrom Windows files. - Don't pass side effects such as
isspace(*p++), because the function may be a macro that evaluates the argument twice. - UTF-8. These functions look at single bytes. Unicode spaces such as the no-break space are not recognized, and neither are non-ASCII letters; use
<wctype.h>(iswspace,iswupper) with wide characters, or a library. - Parsing numbers:
strtolandscanf("%d")already skip leading whitespace, so you rarely need to do it yourself before conversion. - Standard version.
isspace,isupper,islowerare in C89,isblankin C99.