{"id":7614,"date":"2026-06-06T00:01:25","date_gmt":"2026-06-06T07:01:25","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7614"},"modified":"2026-05-30T12:33:18","modified_gmt":"2026-05-30T19:33:18","slug":"ctype-functions-isblank-and-isspace","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7614","title":{"rendered":"Ctype Functions: <em>isblank()<\/em> and <em>isspace()<\/em>"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/05\/ctype.png\" alt=\"\" width=\"400\" height=\"138\" class=\"alignnone size-full wp-image-7590\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/05\/ctype.png 400w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/05\/ctype-300x104.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><br \/>\nYou can draw a blank and you can get spaced out, so there&#8217;s a difference in the English language between the words &#8220;space&#8221; and &#8220;blank.&#8221; This difference doesn&#8217;t explain how the ctype functions <em>isblank()<\/em> and <em>isspace()<\/em> work. In this Lesson, I cover these two functions and their differences, as I continue my exploration of the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7542\">ctype functions<\/a>.<br \/>\n<!--more--><br \/>\nHere are the specific definitions:<\/p>\n<p>The <em>isblank()<\/em> function returns TRUE when the character examined is a space (<code>' '<\/code>) or tab (<code>'\\t'<\/code>). These are considered the &#8220;blank&#8221; characters.<\/p>\n<p>The <em>isspace()<\/em> function returns TRUE for all whitespace characters. These include the space and tab, but also newline (<code>'\\n'<\/code>), carriage return (<code>'\\r'<\/code>), vertical tab (<code>'\\v'<\/code>), and the form-feed (<code>'\\f'<\/code>).<\/p>\n<p>The following program outputs ASCII codes from zero through 32 (0x20), the space character. The <em>isblank(<\/em>) and <em>isspace()<\/em> functions both return FALSE for the DEL character (code 127, 0x7F). I&#8217;m using <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2568\">wide character<\/a> output so that you can see what the various ASCII codes represent as character abbreviations.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_06_06-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2026_06_06-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;locale.h&gt;\r\n#include &lt;wchar.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\nint main()\r\n{\r\n    int ch;\r\n\r\n    <span class=\"comments\">\/* set locale for wide characters *\/<\/span>\r\n    setlocale(LC_ALL,\"\");\r\n\r\n    <span class=\"comments\">\/* output the lower ASCII codes *\/<\/span>\r\n    for( ch=0x00; ch&lt;=' '; ch++ )\r\n    {\r\n        wprintf(L\"%2d %02X %lc is\",ch,ch,ch+9216);\r\n        if( isspace(ch) )\r\n        {\r\n            wprintf(L\" (space)\");\r\n            if( isblank(ch) )\r\n                wprintf(L\" and (blank)\");\r\n        }\r\n        else\r\n        {\r\n            wprintf(L\" non-space\/blank\");\r\n        }\r\n        putwchar(L'\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>for<\/em> 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 <em>isspace()<\/em> function, the word &#8220;space&#8221; is output. If it&#8217;s also a blank, the words &#8220;and blank&#8221; are output. Otherwise, the code is flagged as &#8220;non-space\/blank.&#8221;<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre> 0 00 \u2400 is non-space\/blank\r\n 1 01 \u2401 is non-space\/blank\r\n 2 02 \u2402 is non-space\/blank\r\n 3 03 \u2403 is non-space\/blank\r\n 4 04 \u2404 is non-space\/blank\r\n 5 05 \u2405 is non-space\/blank\r\n 6 06 \u2406 is non-space\/blank\r\n 7 07 \u2407 is non-space\/blank\r\n 8 08 \u2408 is non-space\/blank\r\n 9 09 \u2409 is space and blank\r\n10 0A \u240a is space\r\n11 0B \u240b is space\r\n12 0C \u240c is space\r\n13 0D \u240d is space\r\n14 0E \u240e is non-space\/blank\r\n15 0F \u240f is non-space\/blank\r\n16 10 \u2410 is non-space\/blank\r\n17 11 \u2411 is non-space\/blank\r\n18 12 \u2412 is non-space\/blank\r\n19 13 \u2413 is non-space\/blank\r\n20 14 \u2414 is non-space\/blank\r\n21 15 \u2415 is non-space\/blank\r\n22 16 \u2416 is non-space\/blank\r\n23 17 \u2417 is non-space\/blank\r\n24 18 \u2418 is non-space\/blank\r\n25 19 \u2419 is non-space\/blank\r\n26 1A \u241a is non-space\/blank\r\n27 1B \u241b is non-space\/blank\r\n28 1C \u241c is non-space\/blank\r\n29 1D \u241d is non-space\/blank\r\n30 1E \u241e is non-space\/blank\r\n31 1F \u241f is non-space\/blank\r\n32 20 \u2420 is space and blank<\/pre>\n<p>From the output: HT is horizontal tab (or just &#8220;tab&#8221;); LF is line feed; VT is vertical tab; FF is form-feed; CR is carriage return; and SP is the space character.<\/p>\n<p>For the following update to the code, I&#8217;ve written my own <em>isspace()<\/em> and <em>isblank()<\/em> functions. My <em>isspace()<\/em> function uses a <em>switch-case<\/em> construction to check for the whitespace characters. My <em>isblank()<\/em> function just checks for the tab and space characters. The <em>main()<\/em> function is unchanged.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_06_06-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2026_06_06-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;locale.h&gt;\r\n#include &lt;wchar.h&gt;\r\n#include &lt;stdbool.h&gt;\r\n\r\n#define TAB 0x09\r\n#define LF 0x0A\r\n#define VT 0x0B\r\n#define FF 0x0C\r\n#define CR 0x0D\r\n#define SPACE 0x20\r\n\r\n<span class=\"comments\">\/* returns true for all whitespace chars *\/<\/span>\r\nint isspace(int c)\r\n{\r\n    switch(c)\r\n    {\r\n        case TAB:\r\n        case LF:\r\n        case VT:\r\n        case FF:\r\n        case CR:\r\n        case SPACE:\r\n            return true;\r\n        default:\r\n            return false;\r\n    }\r\n}\r\n\r\n<span class=\"comments\">\/* returns true for tab and space only *\/<\/span>\r\nint isblank(int c)\r\n{\r\n    if( c==TAB || c==SPACE )\r\n        return true;\r\n    else\r\n        return false;\r\n}\r\n\r\nint main()\r\n{\r\n    int ch;\r\n\r\n    <span class=\"comments\">\/* set locale for wide characters *\/<\/span>\r\n    setlocale(LC_ALL,\"\");\r\n\r\n    <span class=\"comments\">\/* output the lower ASCII codes *\/<\/span>\r\n    for( ch=0x00; ch&lt;=' '; ch++ )\r\n    {\r\n        wprintf(L\"%2d %02X %lc is\",ch,ch,ch+9216);\r\n        if( isspace(ch) )\r\n        {\r\n            wprintf(L\" space\");\r\n            if( isblank(ch) )\r\n                wprintf(L\" and blank\");\r\n        }\r\n        else\r\n        {\r\n            wprintf(L\" non-space\/blank\");\r\n        }\r\n        putwchar(L'\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The program&#8217;s output is the same as the first example.<\/p>\n<p>I suppose the reason for having <em>isblank(<\/em>) and <em>isspace()<\/em> 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&#8217;s just a thought. At lease these functions are less confusing than <em>isgraph()<\/em> and <em>isprint()<\/em> (from last week&#8217;s Lesson).<\/p>\n<p>I cover the ctype <em>isascii()<\/em> and <em>iscntrl()<\/em> functions in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Don&#8217;t get spaced out if you&#8217;re drawing a blank! One is merely a subset of the other. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7614\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7614","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7614"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7614\/revisions"}],"predecessor-version":[{"id":7632,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7614\/revisions\/7632"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}