{"id":2953,"date":"2018-02-10T00:01:19","date_gmt":"2018-02-10T08:01:19","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2953"},"modified":"2018-02-17T08:26:43","modified_gmt":"2018-02-17T16:26:43","slug":"ruminations-on-the-_bool-variable-type","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2953","title":{"rendered":"Ruminations on the <em>_Bool<\/em> variable type"},"content":{"rendered":"<p>One of the C language keywords added in the C99 update is <em>_Bool<\/em>. It&#8217;s an underscore keyword, which means it lacks the respect gained by the other keywords &mdash; even the useless ones like <em>auto<\/em>.<br \/>\n<!--more--><br \/>\nThe <em>_Bool<\/em> keyword creates a <em>boolean<\/em> variable type. Boolean, named after English mathematician George Boole, refers to values and operators that deal logic. In programming, it means TRUE and FALSE operations, but also lends itself to the binary values, 0 and 1. A <em>_Bool<\/em> variable type holds either 0 or 1. It&#8217;s one-bit in size.<\/p>\n<blockquote><p><em>Theoretically<\/em>, a <em>_Bool<\/em> is one-bit in size. If you run the <em>sizeof<\/em> keyword on a <em>_Bool<\/em>, you get the value 1, the same as for a <em>char<\/em> variable.<\/p><\/blockquote>\n<p>Internally, who knows what&#8217;s going on with the <em>_Bool<\/em> variable type. It&#8217;s most likely a specialized <em>char<\/em> variable, but that&#8217;s not how the compiler treats it. If you create and use a <em>_Bool<\/em> variable, it&#8217;s value is either 0 or 1. If you try to stuff another value into the variable, it becomes 0 if that value is zero, 1 otherwise.<\/p>\n<p>You can use a <em>_Bool<\/em> variable to act as a toggle, such as demonstrated in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2941\">this month&#8217;s Exercise<\/a>. I devised asolution that uses a <em>_Bool<\/em> variable type; <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/01\/02exercise-bool.c\">click here<\/a> to view it.<\/p>\n<p>The following code demonstrates what happens when you increment a <em>_Bool<\/em> variable:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n  \r\nint main()\r\n{\r\n    _Bool alpha = 0;\r\n    int i;\r\n\r\n    for(i=0;i&lt;10;i++)\r\n    {\r\n        printf(\"%d\\n\",alpha);\r\n        alpha++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p><em>_Bool<\/em> variable <code>alpha<\/code> is incremented in Line 11. Even so, the output shows zero for the first turn of the loop, then ones afterwards:<\/p>\n<p><code>0<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<\/code><\/p>\n<p>If you modify Line 11 to read <code>alpha--;<\/code>, and decrement <em>_Bool<\/em> variable <code>alpha<\/code>, the output changes to this:<\/p>\n<p><code>0<br \/>\n1<br \/>\n0<br \/>\n1<br \/>\n0<br \/>\n1<br \/>\n0<br \/>\n1<br \/>\n0<br \/>\n1<\/code><\/p>\n<p>Before I ran this code, my assumption would be that incrementing or decrementing a <em>_Bool<\/em> variable toggles between 1 and 0. After all, that&#8217;s how a bit works inside the computer. But in C, incrementing a <em>_Bool<\/em> variable is interpreted as positive, so 1 is returned. Decrementing &#8220;wraps&#8221; the value to 1, which is then decremented back to zero.<\/p>\n<p>I explore more tricks with the <em>_Bool<\/em> variable type in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2956\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s valid and funky, though a lot of C programmers don&#8217;t bother with the <em>_Bool<\/em> type variable. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2953\">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-2953","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\/2953","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=2953"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2953\/revisions"}],"predecessor-version":[{"id":2977,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2953\/revisions\/2977"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}