{"id":7457,"date":"2026-05-09T00:01:51","date_gmt":"2026-05-09T07:01:51","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7457"},"modified":"2026-05-02T13:10:24","modified_gmt":"2026-05-02T20:10:24","slug":"ctype-functions-toupper-and-tolower","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7457","title":{"rendered":"Ctype Functions: <em>toupper()<\/em> and <em>tolower()<\/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 easiest way to mess with characters in C is to employ the <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7542\">ctype functions<\/a>. I name this category of library functions after the <code>ctype.h<\/code> header file that defines them. Some are true functions, some are macros. Each function can be replicated with standard C code, though it&#8217;s quicker to use an established function than to code your own. But that&#8217;s not the point of learning to program!<br \/>\n<!--more--><br \/>\nThe following code demonstrates the <em>toupper()<\/em> and <em>tolower()<\/em> ctype functions. As with all ctype functions, these functions work with with single characters. These two ctype functions are unique, however, in that they return values other than TRUE and FALSE: The <em>toupper()<\/em> function returns the uppercase character of the argument passed; <em>tolower()<\/em> returns the lowercase character. These functions have no effect on non-alphabetic characters, returning the character passed as-is.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_05_09-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2026_05_09-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\nint main()\r\n{\r\n    char string[] = \"Nothing beats knowing the ASCII codes!\\n\";\r\n    char *s;\r\n\r\n    <span class=\"comments\">\/* uppercase *\/<\/span>\r\n    s = string;\r\n    while( *s != '\\0' )\r\n    {\r\n        putchar( toupper(*s) );\r\n        s++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* lowercase *\/<\/span>\r\n    s = string;\r\n    while( *s != '\\0' )\r\n    {\r\n        putchar( tolower(*s) );\r\n        s++;\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The two <em>while<\/em> loops output the characters in array <code>string[]<\/code>. The first loop sends the character through the <em>toupper()<\/em> function; the second loop sends the character through the <em>tolower()<\/em> function. Here&#8217;s the output:<\/p>\n<pre>NOTHING BEATS KNOWING THE ASCII CODES!\r\nnothing beats knowing the ascii codes!<\/pre>\n<p>If you&#8217;re going to emulate these functions, it helps to know the ASCII code values for letters A through Z. The ASCII table is organized in such a fashion that uppercase and lowercase letters differ by a single bit, as shown in the following table:<\/p>\n<table>\n<tr>\n<td>A<\/td>\n<td>65<\/td>\n<td>0x41<\/td>\n<td>0100 0001<\/td>\n<td>a<\/td>\n<td>97<\/td>\n<td>0x61<\/td>\n<td>0110 0001<\/td>\n<\/tr>\n<tr>\n<td>B<\/td>\n<td>66<\/td>\n<td>0x42<\/td>\n<td>0100 0010<\/td>\n<td>b<\/td>\n<td>98<\/td>\n<td>0x62<\/td>\n<td>0110 0010<\/td>\n<\/tr>\n<tr>\n<td>C<\/td>\n<td>67<\/td>\n<td>0x43<\/td>\n<td>0100 0011<\/td>\n<td>c<\/td>\n<td>99<\/td>\n<td>0x63<\/td>\n<td>0110 0011<\/td>\n<\/tr>\n<tr>\n<td>D<\/td>\n<td>68<\/td>\n<td>0x44<\/td>\n<td>0100 0100<\/td>\n<td>d<\/td>\n<td>100<\/td>\n<td>0x64<\/td>\n<td>0110 0100<\/td>\n<\/tr>\n<tr>\n<td>E<\/td>\n<td>69<\/td>\n<td>0x45<\/td>\n<td>0100 0101<\/td>\n<td>e<\/td>\n<td>101<\/td>\n<td>0x65<\/td>\n<td>0110 0101<\/td>\n<\/tr>\n<tr>\n<td>F<\/td>\n<td>70<\/td>\n<td>0x46<\/td>\n<td>0100 0110<\/td>\n<td>f<\/td>\n<td>102<\/td>\n<td>0x66<\/td>\n<td>0110 0110<\/td>\n<\/tr>\n<tr>\n<td>G<\/td>\n<td>71<\/td>\n<td>0x47<\/td>\n<td>0100 0111<\/td>\n<td>g<\/td>\n<td>103<\/td>\n<td>0x67<\/td>\n<td>0110 0111<\/td>\n<\/tr>\n<tr>\n<td>H<\/td>\n<td>72<\/td>\n<td>0x48<\/td>\n<td>0100 1000<\/td>\n<td>h<\/td>\n<td>104<\/td>\n<td>0x68<\/td>\n<td>0110 1000<\/td>\n<\/tr>\n<tr>\n<td>I<\/td>\n<td>73<\/td>\n<td>0x49<\/td>\n<td>0100 1001<\/td>\n<td>i<\/td>\n<td>105<\/td>\n<td>0x69<\/td>\n<td>0110 1001<\/td>\n<\/tr>\n<tr>\n<td>J<\/td>\n<td>74<\/td>\n<td>0x4A<\/td>\n<td>0100 1010<\/td>\n<td>j<\/td>\n<td>106<\/td>\n<td>0x6A<\/td>\n<td>0110 1010<\/td>\n<\/tr>\n<tr>\n<td>K<\/td>\n<td>75<\/td>\n<td>0x4B<\/td>\n<td>0100 1011<\/td>\n<td>k<\/td>\n<td>107<\/td>\n<td>0x6B<\/td>\n<td>0110 1011<\/td>\n<\/tr>\n<tr>\n<td>L<\/td>\n<td>76<\/td>\n<td>0x4C<\/td>\n<td>0100 1100<\/td>\n<td>l<\/td>\n<td>108<\/td>\n<td>0x6C<\/td>\n<td>0110 1100<\/td>\n<\/tr>\n<tr>\n<td>M<\/td>\n<td>77<\/td>\n<td>0x4D<\/td>\n<td>0100 1101<\/td>\n<td>m<\/td>\n<td>109<\/td>\n<td>0x6D<\/td>\n<td>0110 1101<\/td>\n<\/tr>\n<tr>\n<td>N<\/td>\n<td>78<\/td>\n<td>0x4E<\/td>\n<td>0100 1110<\/td>\n<td>n<\/td>\n<td>110<\/td>\n<td>0x6E<\/td>\n<td>0110 1110<\/td>\n<\/tr>\n<tr>\n<td>O<\/td>\n<td>79<\/td>\n<td>0x4F<\/td>\n<td>0100 1111<\/td>\n<td>o<\/td>\n<td>111<\/td>\n<td>0x6F<\/td>\n<td>0110 1111<\/td>\n<\/tr>\n<tr>\n<td>P<\/td>\n<td>80<\/td>\n<td>0x50<\/td>\n<td>0101 0000<\/td>\n<td>p<\/td>\n<td>112<\/td>\n<td>0x70<\/td>\n<td>0111 0000<\/td>\n<\/tr>\n<tr>\n<td>Q<\/td>\n<td>81<\/td>\n<td>0x51<\/td>\n<td>0101 0001<\/td>\n<td>q<\/td>\n<td>113<\/td>\n<td>0x71<\/td>\n<td>0111 0001<\/td>\n<\/tr>\n<tr>\n<td>R<\/td>\n<td>82<\/td>\n<td>0x52<\/td>\n<td>0101 0010<\/td>\n<td>r<\/td>\n<td>114<\/td>\n<td>0x72<\/td>\n<td>0111 0010<\/td>\n<\/tr>\n<tr>\n<td>S<\/td>\n<td>83<\/td>\n<td>0x53<\/td>\n<td>0101 0011<\/td>\n<td>s<\/td>\n<td>115<\/td>\n<td>0x73<\/td>\n<td>0111 0011<\/td>\n<\/tr>\n<tr>\n<td>T<\/td>\n<td>84<\/td>\n<td>0x54<\/td>\n<td>0101 0100<\/td>\n<td>t<\/td>\n<td>116<\/td>\n<td>0x74<\/td>\n<td>0111 0100<\/td>\n<\/tr>\n<tr>\n<td>U<\/td>\n<td>85<\/td>\n<td>0x55<\/td>\n<td>0101 0101<\/td>\n<td>u<\/td>\n<td>117<\/td>\n<td>0x75<\/td>\n<td>0111 0101<\/td>\n<\/tr>\n<tr>\n<td>V<\/td>\n<td>86<\/td>\n<td>0x56<\/td>\n<td>0101 0110<\/td>\n<td>v<\/td>\n<td>118<\/td>\n<td>0x76<\/td>\n<td>0111 0110<\/td>\n<\/tr>\n<tr>\n<td>W<\/td>\n<td>87<\/td>\n<td>0x57<\/td>\n<td>0101 0111<\/td>\n<td>w<\/td>\n<td>119<\/td>\n<td>0x77<\/td>\n<td>0111 0111<\/td>\n<\/tr>\n<tr>\n<td>X<\/td>\n<td>88<\/td>\n<td>0x58<\/td>\n<td>0101 1000<\/td>\n<td>x<\/td>\n<td>120<\/td>\n<td>0x78<\/td>\n<td>0111 1000<\/td>\n<\/tr>\n<tr>\n<td>Y<\/td>\n<td>89<\/td>\n<td>0x59<\/td>\n<td>0101 1001<\/td>\n<td>y<\/td>\n<td>121<\/td>\n<td>0x79<\/td>\n<td>0111 1001<\/td>\n<\/tr>\n<tr>\n<td>Z<\/td>\n<td>90<\/td>\n<td>0x5A<\/td>\n<td>0101 1010<\/td>\n<td>z<\/td>\n<td>122<\/td>\n<td>0x7A<\/td>\n<td>0111 1010<\/td>\n<\/tr>\n<\/table>\n<p>Several clever and fun approaches are available for changing uppercase to lowercase, one of which involves setting or resetting the fifth bit. But for my emulation functions, I use a comparison:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_05_09-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2026_05_09-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n<span class=\"comments\">\/* convert to uppercase *\/<\/span>\r\nint toupper(int c)\r\n{\r\n    if( c&gt;='a' &amp;&amp; c&lt;='z' )\r\n        c -= 32;\r\n\r\n    return(c);\r\n}\r\n\r\n<span class=\"comments\">\/* convert to lowercase *\/<\/span>\r\nint tolower(int c)\r\n{\r\n    if( c&gt;='A' &amp;&amp; c&lt;='Z' )\r\n        c += 32;\r\n\r\n    return(c);\r\n}\r\n\r\nint main()\r\n{\r\n    char string[] = \"Nothing beats knowing the ASCII codes!\\n\";\r\n    char *s;\r\n\r\n    <span class=\"comments\">\/* uppercase *\/<\/span>\r\n    s = string;\r\n    while( *s != '\\0' )\r\n    {\r\n        putchar( toupper(*s) );\r\n        s++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* lowercase *\/<\/span>\r\n    s = string;\r\n    while( *s != '\\0' )\r\n    {\r\n        putchar( tolower(*s) );\r\n        s++;\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This update to the code removes including the <code>ctype.h<\/code> header file. Instead, I wrote my own <em>toupper()<\/em> and <em>tolower()<\/em> functions, following the ctype format. Each of my emulations works the same: An <em>if<\/em> test checks for a letter of the alphabet, upper- or lowercase. If true, the value 32 is subtracted from the character&#8217;s value to convert it to uppercase; the value 32 is added to the character&#8217;s value to convert it to lowercase. Otherwise, the same character is returned.<\/p>\n<p>The program&#8217;s output is the same:<\/p>\n<pre>NOTHING BEATS KNOWING THE ASCII CODES!\r\nnothing beats knowing the ascii codes!<\/pre>\n<p>In the <em>toupper()<\/em> function, you could replace the expression <code>c -=  32<\/code> with <code>c &amp;= 0xDF<\/code> By resetting the fifth bit, you convert a lowercase letter to uppercase. Likewise, in the <em>tolower()<\/em> function you can replace <code>c += 32<\/code> with <code>c |=0x20<\/code> These modifications work, relying upon bit manipulation to set or reset the bits to make the character conversion. I don&#8217;t know which approach is used internally by the C library, though either way works.<\/p>\n<p>Next week I explore the <em>isupper()<\/em> and <em>islower()<\/em> functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Character manipulation functions are handy &mdash; and easy to replicate. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7457\">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-7457","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\/7457","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=7457"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7457\/revisions"}],"predecessor-version":[{"id":7593,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7457\/revisions\/7593"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}