{"id":4904,"date":"2021-08-07T00:01:17","date_gmt":"2021-08-07T07:01:17","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4904"},"modified":"2021-08-01T10:25:31","modified_gmt":"2021-08-01T17:25:31","slug":"characters-values-and-playing-with-your-brain","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4904","title":{"rendered":"Characters, Values, and Playing with Your Brain"},"content":{"rendered":"<p>I&#8217;ve messed with characters as values quite a few times in my code. Keeping in mind that the <em>char<\/em> data type is really a tiny integer value, you can perform all kinds of tricks &mdash; stuff that drives non-programmers crazy.<br \/>\n<!--more--><br \/>\nFor example, I often do math using character values. Consider that <em>char<\/em> variable <code>a<\/code> is a digit, 0 through 9:<\/p>\n<p><code>b = a - '0';<\/code><\/p>\n<p>This statement subtracts character <code>'0'<\/code> from the value of variable <code>a<\/code>. Seems crazy, but what&#8217;s really going on is the ASCII code value for <code>'0'<\/code> (48 decimal, 0x30 hex) is subtracted from variable <code>a<\/code>. Because variable <code>a<\/code> holds a digit, the result is the value 0 through 9, which is stored in variable <code>b<\/code>. This technique is one way to convert character numbers into their integer values.<\/p>\n<p>Likewise, you can convert a letter of the alphabet to an integer value 0 through 26 for A through Z:<\/p>\n<p><code>b = a - 'A';<\/code><\/p>\n<p>All this nonsense works because to the computer, a character is really a visual representation of a value. This interpretation leads to some interesting possibilities, as shown in the following code.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_08_07-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_08_07-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int t['A'];\r\n\r\n    printf(\"The array contains %ld elements\\n\",sizeof(t)\/sizeof(int));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 5, <em>int<\/em> array <code>t[]<\/code> is defined, which contains <code>'A'<\/code> elements. This statement is valid in C because the <code>'A'<\/code> is converted by the compiler into its integer value. To see how many elements are in the array, run the program:<\/p>\n<p><code>The array contains 65 elements<\/code><\/p>\n<p>If you look up the value of <code>'A'<\/code> on an ASCII table (see <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4895\">this month&#8217;s Exercise<\/a>), you discover that it&#8217;s 65. So the declaration <code>int t['A']<\/code> creates an array of 65 elements, the ASCII value of character <code>'A'<\/code>.<\/p>\n<p>By the way, the array size is determined by dividing the amount of memory occupied by the array, <code>sizeof(t)<\/code>, by the size of the array&#8217;s data type, integer <code>sizeof(int)<\/code>. The result yields the number of elements in the array.<\/p>\n<p>Using characters as integers is a fun trick, one that can throw off a beginning C programmer. The problem with this trick is that it obfuscates the code. It&#8217;s much easier to write <code>int t[65]<\/code> than <code>int t['A']<\/code>. But it&#8217;s cute, so I thought I&#8217;d point it out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Though we humans see a character as a letter or symbol, it&#8217;s really a value as far as the compiler is concerned. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4904\">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-4904","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\/4904","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=4904"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4904\/revisions"}],"predecessor-version":[{"id":4926,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4904\/revisions\/4926"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}