{"id":7585,"date":"2026-05-23T00:01:32","date_gmt":"2026-05-23T07:01:32","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7585"},"modified":"2026-05-16T20:25:41","modified_gmt":"2026-05-17T03:25:41","slug":"ctype-functions-isalnum-isalpha-and-isdigit","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7585","title":{"rendered":"Ctype Functions: <em>isalnum()<\/em>, <em>isalpha()<\/em> and <em>isdigit()<\/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 \/>\nThe ctype functions <em>isalnum()<\/em>, <em>isalpha()<\/em>, and <em>isdigit()<\/em> examine the alphanumeric ASCII characters. Each of these functions are related, so I cover them together in this post that continues my exploration of the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7542\">ctype functions<\/a>.<br \/>\n<!--more--><br \/>\nThe relationship between these functions works because the positive results of <em>isalpha()<\/em> and <em>idigit()<\/em> are useful in crafting the <em>isalnum()<\/em> function: If a character is both alphabetic (<em>isalpha()<\/em>) and a digit (<em>isdigit()<\/em>), then it&#8217;s also alphanumeric (<em>isalnum()<\/em>).<\/p>\n<p>The following code outputs the printable ASCII characters, from the space (code 32 or 0x20) through the DEL (code 127 or 0x7F). ASCII code 127 is &#8220;DEL&#8221; or delete, which doesn&#8217;t print but isn&#8217;t the same as a space. (More on this condition in another Lesson.) For each line, text is output to describe whether the character is alphanumeric and if so whether it&#8217;s alphabetic or a digit.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_05_23-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2026_05_23-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\n#define DEL 127\r\n\r\nint main()\r\n{\r\n    int ch;\r\n\r\n    for( ch=' '; ch&lt;=DEL; ch++ )\r\n    {\r\n        printf(\"%3d %2X '%c' is\",ch,ch,ch);\r\n        if( isalnum(ch) )\r\n        {\r\n            printf(\" alphanumeric\");\r\n            if( isalpha(ch) )\r\n                printf(\" (alphabetic)\");\r\n            if( isdigit(ch) )\r\n                printf(\" (digit)\");\r\n        }\r\n        else\r\n        {\r\n            printf(\" non-alphanumeric\");\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The code uses the three ctype functions: <em>isalnum()<\/em>, <em>isalpha()<\/em>, and <em>isdigit()<\/em>. A <em>for<\/em> loop plows through characters <code>' '<\/code> through <code>'~'<\/code>. Various <em>printf()<\/em> statements output the qualities of these characters, specifically tagging alphanumeric and non-alphanumeric, which alphabetic and digit characters called out separately.<\/p>\n<p>I&#8217;m unhappy with the nested <em>if<\/em> statements, but given the condition that the character is either alphabetic or digit, it works. And, yes, it could be an <em>if-else<\/em> statement but that would remove the <em>isdigit()<\/em> test.<\/p>\n<p>Here&#8217;s abbreviated output:<\/p>\n<pre> 32 20 ' ' is non-alphanumeric\r\n 33 21 '!' is non-alphanumeric\r\n 34 22 '\"' is non-alphanumeric\r\n 35 23 '#' is non-alphanumeric\r\n...\r\n 46 2E '.' is non-alphanumeric\r\n 47 2F '\/' is non-alphanumeric\r\n 48 30 '0' is alphanumeric (digit)\r\n 49 31 '1' is alphanumeric (digit)\r\n 50 32 '2' is alphanumeric (digit)\r\n 51 33 '3' is alphanumeric (digit)\r\n 52 34 '4' is alphanumeric (digit)\r\n 53 35 '5' is alphanumeric (digit)\r\n 54 36 '6' is alphanumeric (digit)\r\n 55 37 '7' is alphanumeric (digit)\r\n 56 38 '8' is alphanumeric (digit)\r\n 57 39 '9' is alphanumeric (digit)\r\n 58 3A ':' is non-alphanumeric\r\n 59 3B ';' is non-alphanumeric\r\n...\r\n 63 3F '?' is non-alphanumeric\r\n 64 40 '@' is non-alphanumeric\r\n 65 41 'A' is alphanumeric (alphabetic)\r\n 66 42 'B' is alphanumeric (alphabetic)\r\n 67 43 'C' is alphanumeric (alphabetic)\r\n...\r\n 89 59 'Y' is alphanumeric (alphabetic)\r\n 90 5A 'Z' is alphanumeric (alphabetic)\r\n 91 5B '[' is non-alphanumeric\r\n 92 5C '\\' is non-alphanumeric\r\n 93 5D ']' is non-alphanumeric\r\n 94 5E '^' is non-alphanumeric\r\n 95 5F '_' is non-alphanumeric\r\n 96 60 '`' is non-alphanumeric\r\n 97 61 'a' is alphanumeric (alphabetic)\r\n 98 62 'b' is alphanumeric (alphabetic)\r\n...\r\n121 79 'y' is alphanumeric (alphabetic)\r\n122 7A 'z' is alphanumeric (alphabetic)\r\n123 7B '{' is non-alphanumeric\r\n124 7C '|' is non-alphanumeric\r\n125 7D '}' is non-alphanumeric\r\n126 7E '~' is non-alphanumeric\r\n127 7F '' is non-alphanumeric<\/pre>\n<p>Coding your own version of these ctype functions requires some basic comparisons, as shown in the following code. Note that I use the results of both <em>isalpha()<\/em> and <em>isdigit()<\/em> to craft my <em>isalnum()<\/em> function:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_05_23-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2026_05_23-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define TRUE 1\r\n#define FALSE 0\r\n#define DEL 127\r\n\r\nint isalpha(int c)\r\n{\r\n    if( (c&gt;='A' &amp;&amp; c&lt;='Z') || (c&gt;='a' &amp;&amp; c&lt;='z') )\r\n            return TRUE;\r\n    return FALSE;\r\n}\r\n\r\nint isdigit(int c)\r\n{\r\n    if( c&gt;='0' &amp;&amp; c&lt;='9' )\r\n        return TRUE;\r\n    return FALSE;\r\n}\r\n\r\nint isalnum(int c)\r\n{\r\n    if( isalpha(c) || isdigit(c) )\r\n        return TRUE;\r\n    return FALSE;\r\n}\r\n\r\nint main()\r\n{\r\n    int ch;\r\n\r\n    for( ch=' '; ch&lt;=DEL; ch++ )\r\n    {\r\n        printf(\"%3d %2X '%c' is\",ch,ch,ch);\r\n        if( isalnum(ch) )\r\n        {\r\n            printf(\" alphanumeric\");\r\n            if( isalpha(ch) )\r\n                printf(\" (alphabetic)\");\r\n            if( isdigit(ch) )\r\n                printf(\" (digit)\");\r\n        }\r\n        else\r\n        {\r\n            printf(\" non-alphanumeric\");\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>isalpha()<\/em> function perform a complex comparison to ensure that the character is in the range <code>'A'<\/code> through <code>'Z'<\/code> or <code>'a'<\/code> through <code>'z'<\/code>. When either condition is true, the character is alphabetic, TRUE is returned.<\/p>\n<p>The <em>isdigit()<\/em> function returns TRUE when the character is in the range of &#8216;0&#8217; through &#8216;9&#8217;.<\/p>\n<p>Finally, the <em>isalnum()<\/em> function determines whether the character is alphabetic (<em>isalpha()<\/em>) or numeric (<em>isdigit()<\/em>) and returns TRUE is so.<\/p>\n<p>The program&#8217;s output is the same.<\/p>\n<p>In next week&#8217;s Lesson I cover the is <em>isgraph()<\/em> and <em>isprint()<\/em> ctype functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>These ctype testing functions work on letters of the alphabet and numeric digits. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7585\">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-7585","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\/7585","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=7585"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7585\/revisions"}],"predecessor-version":[{"id":7599,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7585\/revisions\/7599"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}