{"id":209,"date":"2013-09-07T00:01:23","date_gmt":"2013-09-07T08:01:23","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=209"},"modified":"2013-09-06T08:54:49","modified_gmt":"2013-09-06T16:54:49","slug":"the-c-variable-myth","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=209","title":{"rendered":"The C Variable Myth"},"content":{"rendered":"<p>A variable in C is a myth. Oh, yeah, it&#8217;s a location in memory. That&#8217;s pretty much it. After declaring the variable, the compiler &#8212; and you, the programmer &#8212; pretty much rely upon faith that the variable works and can actually be useful.<br \/>\n<!--more--><br \/>\nI confess that this topic is weird. Higher-level languages have more of a lock on the variable types. When you declare a string or an integer in those languages, the compiler asserts that declaration everywhere. In C, however, a variable type can be silly putty, with the emphasis on <em>putty<\/em>.<\/p>\n<p>For example:<\/p>\n<p><code>int x;<\/code><\/p>\n<p>The above declaration directs the program to set aside several bytes of storage in which an integer value can be stored. On modern computers, that&#8217;s usually four bytes of storage. (Back when I learned C, it was two.)<\/p>\n<p>The variable <code>x<\/code> isn&#8217;t initialized; its value isn&#8217;t set to zero or anything else. It&#8217;s basically four bytes of memory at a certain address. You can assign the variable a value, but you can also use it uninitialized to see what&#8217;s lurking in memory. (Uninitialized values are considered garbage.) Most programmers would opt to assign <code>x<\/code> a value and then use that integer for some useful purpose.<\/p>\n<p>The myth part comes from treating <code>x<\/code> as an integer. Specifically, the code above declares <code>x<\/code> as a <em>signed<\/em> integer value by default. You could also tell the compiler to treat <code>x<\/code> as an <em>unsigned<\/em> integer by declaring it like this:<\/p>\n<p><code>unsigned x<\/code>;<\/p>\n<p>The above declaration does nothing more to the four bytes of storage that the <em>signed<\/em> declaration does. It&#8217;s still just four bytes of storage lurking somewhere in memory. What matters &#8212; the mythical part &#8212; is how you treat the variable in your code, <em>signed<\/em> or <em>unsigned<\/em>.<\/p>\n<p>Specifically, if a function requires an <em>unsigned<\/em> value, the above declaration wouldn&#8217;t generate a compiler warning. But when you use a <em>signed int<\/em> when a function requires an <em>unsigned int<\/em>, you get a warning. It&#8217;s not an error because the code compiles, although the result may be what you want.<\/p>\n<p>That&#8217;s one mythical aspect of <em>signed<\/em> and <em>unsigned<\/em> variables. Another is which <em>printf()<\/em> conversion character is used to display the variable: The <code>%d<\/code> character is used for <em>signed int<\/em> values; <code>%u<\/code> for <em>unsigned<\/em>. But you can swap those conversion characters willy-nilly and the compiler won&#8217;t balk.<\/p>\n<p>Well, it might, but it doesn&#8217;t matter because the variable is just composed of bits floating inside a four-byte frame somewhere in memory. The conversion character merely interprets the value. Remember that, and consider the following code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    unsigned char a;\r\n    signed char b;\r\n    int x;\r\n\r\n    a = b = 0;\r\n    for(x=0;x&lt;400;x++)\r\n    {\r\n        printf(\"%3u\\t%3d\\n\",a,b);\r\n        a++; b++;\r\n    }\r\n    return(0);\r\n}<\/pre>\n<p>I&#8217;ve used <em>char<\/em> variables in the code above because they&#8217;re smaller than an <em>int<\/em>, one byte instead of four. The range for a <em>signed char<\/em> goes from -128 through 127; an <em>unsigned char<\/em> goes from 0 through 255. The output confirms that:<\/p>\n<pre><code>  0\t  0\r\n  1\t  1\r\n  2\t  2\r\n  3\t  3<\/code><\/pre>\n<p>On up to&#8230;<\/p>\n<pre><code>126\t126\r\n127\t127\r\n128\t-128\r\n129\t-127\r\n130\t-126<\/code><\/pre>\n<p>The value 128 is interpreted by <code>%u<\/code> as <em>unsigned<\/em> for variable <code>a<\/code>, but <em>signed<\/em> <code>%d<\/code> for variable <code>b<\/code>. The same bit value is stored in memory, 128, but the conversion characters determine how it shows up.<\/p>\n<p>Hold that thought.<\/p>\n<p>Because the range on a <em>char<\/em> variable is limited, you see its value roll over when <code>x<\/code> reaches 256:<\/p>\n<pre><code>253\t -3\r\n254\t -2\r\n255\t -1\r\n  0\t  0\r\n  1\t  1\r\n  2\t  2<\/code><\/pre>\n<p>I view the value (256) as being lopped off when its stored in the <em>char<\/em> variable: <code>x<\/code> holds the value 256 but that many bits can fit into the <code>a<\/code> or <code>b<\/code> variable&#8217;s container, so the value is truncated.<\/p>\n<p>Now pick up the thought I told you to hold a few paragraphs back. Edit the code, specifically Line 12, to read:<\/p>\n<p><code>printf(\"%3d\\t%3u\\n\",a,b);<\/code><\/p>\n<p>I&#8217;m swapping the <code>%3d<\/code> and <code>%3u<\/code> in the <em>printf()<\/em> statement.<\/p>\n<p>Next week I&#8217;ll explain what happened and why you see this type of output:<\/p>\n<pre><code>140\t4294967180\r\n141\t4294967181\r\n142\t4294967182\r\n143\t4294967183<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C language variables exist because you believe in them. Treat them properly, and they&#8217;ll do a good job. Mess with them and you&#8217;re head will explode. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=209\">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-209","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\/209","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=209"}],"version-history":[{"count":13,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":281,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions\/281"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}