Ctype Functions: isblank() and isspace()


You can draw a blank and you can get spaced out, so there’s a difference in the English language between the words “space” and “blank.” This difference doesn’t explain how the ctype functions isblank() and isspace() work. In this Lesson, I cover these two functions and their differences, as I continue my exploration of the ctype functions.

Here are the specific definitions:

The isblank() function returns TRUE when the character examined is a space (' ') or tab ('\t'). These are considered the “blank” characters.

The isspace() function returns TRUE for all whitespace characters. These include the space and tab, but also newline ('\n'), carriage return ('\r'), vertical tab ('\v'), and the form-feed ('\f').

The following program outputs ASCII codes from zero through 32 (0x20), the space character. The isblank() and isspace() functions both return FALSE for the DEL character (code 127, 0x7F). I’m using wide character output so that you can see what the various ASCII codes represent as character abbreviations.

2026_06_06-Lesson-a.c

#include <locale.h>
#include <wchar.h>
#include <ctype.h>

int main()
{
    int ch;

    /* set locale for wide characters */
    setlocale(LC_ALL,"");

    /* output the lower ASCII codes */
    for( ch=0x00; ch<=' '; ch++ )
    {
        wprintf(L"%2d %02X %lc is",ch,ch,ch+9216);
        if( isspace(ch) )
        {
            wprintf(L" (space)");
            if( isblank(ch) )
                wprintf(L" and (blank)");
        }
        else
        {
            wprintf(L" non-space/blank");
        }
        putwchar(L'\n');
    }

    return 0;
}

The for loop churns through ASFCII codes zero through space. For each character, its decimal and hex values, as well as its Unicode character, are output. If the code qualifies as a space, according to the isspace() function, the word “space” is output. If it’s also a blank, the words “and blank” are output. Otherwise, the code is flagged as “non-space/blank.”

Here’s a sample run:

 0 00 ␀ is non-space/blank
 1 01 ␁ is non-space/blank
 2 02 ␂ is non-space/blank
 3 03 ␃ is non-space/blank
 4 04 ␄ is non-space/blank
 5 05 ␅ is non-space/blank
 6 06 ␆ is non-space/blank
 7 07 ␇ is non-space/blank
 8 08 ␈ is non-space/blank
 9 09 ␉ is space and blank
10 0A ␊ is space
11 0B ␋ is space
12 0C ␌ is space
13 0D ␍ is space
14 0E ␎ is non-space/blank
15 0F ␏ is non-space/blank
16 10 ␐ is non-space/blank
17 11 ␑ is non-space/blank
18 12 ␒ is non-space/blank
19 13 ␓ is non-space/blank
20 14 ␔ is non-space/blank
21 15 ␕ is non-space/blank
22 16 ␖ is non-space/blank
23 17 ␗ is non-space/blank
24 18 ␘ is non-space/blank
25 19 ␙ is non-space/blank
26 1A ␚ is non-space/blank
27 1B ␛ is non-space/blank
28 1C ␜ is non-space/blank
29 1D ␝ is non-space/blank
30 1E ␞ is non-space/blank
31 1F ␟ is non-space/blank
32 20 ␠ is space and blank

From the output: HT is horizontal tab (or just “tab”); LF is line feed; VT is vertical tab; FF is form-feed; CR is carriage return; and SP is the space character.

For the following update to the code, I’ve written my own isspace() and isblank() functions. My isspace() function uses a switch-case construction to check for the whitespace characters. My isblank() function just checks for the tab and space characters. The main() function is unchanged.

2026_06_06-Lesson-b.c

#include <locale.h>
#include <wchar.h>
#include <stdbool.h>

#define TAB 0x09
#define LF 0x0A
#define VT 0x0B
#define FF 0x0C
#define CR 0x0D
#define SPACE 0x20

/* returns true for all whitespace chars */
int isspace(int c)
{
    switch(c)
    {
        case TAB:
        case LF:
        case VT:
        case FF:
        case CR:
        case SPACE:
            return true;
        default:
            return false;
    }
}

/* returns true for tab and space only */
int isblank(int c)
{
    if( c==TAB || c==SPACE )
        return true;
    else
        return false;
}

int main()
{
    int ch;

    /* set locale for wide characters */
    setlocale(LC_ALL,"");

    /* output the lower ASCII codes */
    for( ch=0x00; ch<=' '; ch++ )
    {
        wprintf(L"%2d %02X %lc is",ch,ch,ch+9216);
        if( isspace(ch) )
        {
            wprintf(L" space");
            if( isblank(ch) )
                wprintf(L" and blank");
        }
        else
        {
            wprintf(L" non-space/blank");
        }
        putwchar(L'\n');
    }

    return 0;
}

The program’s output is the same as the first example.

I suppose the reason for having isblank() and isspace() functions is to be specific with textual blanks as opposed to whitespace characters. For example, a tab and a space would appear in a paragraph whereas whitespace can be anywhere on a page. That’s just a thought. At lease these functions are less confusing than isgraph() and isprint() (from last week’s Lesson).

I cover the ctype isascii() and iscntrl() functions in next week’s Lesson.

One thought on “Ctype Functions: isblank() and isspace()

Leave a Reply