{"id":2956,"date":"2018-02-17T00:01:34","date_gmt":"2018-02-17T08:01:34","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2956"},"modified":"2018-02-10T10:22:29","modified_gmt":"2018-02-10T18:22:29","slug":"more-fun-with-_bool","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2956","title":{"rendered":"More Fun with <em>_Bool<\/em>"},"content":{"rendered":"<p>Plenty of C coders implemented boolean variables long before the C99 standard introduced the <em>_Bool<\/em> type. Programmers defined TRUE and FALSE constants, they used <em>char<\/em> or <em>short<\/em> variables as toggles, or they manipulated bits in a byte to simulate binary and boolean operations. With all that going on, you&#8217;d think adding a boolean variable type to C and calling it <em>bool<\/em> would have been a no-brainer. It wasn&#8217;t.<br \/>\n<!--more--><br \/>\nThe problem is that existing programs may have defined a variable as <code>bool<\/code>. This is most likely the reason why the new C language keywords (introduced in the C99 and C11 standards) are prefixed with an underscore.<\/p>\n<p>Underscores in C are significant, though not officially. Many internal functions, variable names, and definitions use one or more underscores in their names. If you&#8217;re an experienced programmer and you see something like <code>__var<\/code>, you know that it&#8217;s some internal value you probably don&#8217;t want to mess with it. Given this condition, it&#8217;s curious to me why the C overlords decided to create new keyworks with an underscore prefix. It&#8217;s almost like a subtle message <em>not<\/em> to use the things.<\/p>\n<p>Yet, you can still use <em>bool<\/em> as a variable type. You just have to include the <code>stdbool.h<\/code> header file in your code. This header file defines <em>bool<\/em> as a variable type <em>_Bool<\/em>. It&#8217;s not a <em>typecast<\/em>, it&#8217;s a <code>#define<\/code>.<\/p>\n<p>Also defined in the <code>stdbool.h<\/code> header are <em>true<\/em> and <em>false<\/em>, 1 and 0 respectively.<\/p>\n<p>Here is the decrementing code from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2948\">last week&#8217;s Lesson<\/a>, but using the <em>bool<\/em> definition:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdbool.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>The output is the same as when the <em>_Bool<\/em> keyword is used.<\/p>\n<p>You can also make <em>_Bool<\/em> arrays:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n  \r\nint main()\r\n{\r\n    _Bool a[8] = { 0, 0, 1, 0, 1, 0, 1, 0 };\r\n    int i;\r\n\r\n    for(i=0;i&lt;8;i++)\r\n        printf(\"%d \",a[i]);\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>A <em>_Bool<\/em> array <code>a[]<\/code> is created and elements assigned at Line 6. The values are printed by using the <code>a[i]<\/code> notation in the <em>for<\/em> loop at Line 9. Here&#8217;s the output:<\/p>\n<pre><code>0 0 1 0 1 0 1 0<\/code><\/pre>\n<p>I think it would be clever if the C library had functions to map a boolean array to an integer type, though you&#8217;re free to code such functions on your own. \ud83d\ude00<\/p>\n<p>And, as you may have feared, you can use <em>_Bool<\/em> pointers as well:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{ \r\n    _Bool *aptr;\r\n    int i;\r\n\r\n    <span class=\"comments\">\/* allocate boolean storage *\/<\/span>\r\n    aptr = (_Bool *)malloc( 8 * sizeof(_Bool) );\r\n    if( aptr == NULL )\r\n    {\r\n        puts(\"Unable to allocate _Bool memory?\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* fill and display values *\/<\/span>\r\n    for(i=0;i&lt;8;i++)\r\n    {\r\n        *(aptr+i) = 1;\r\n        printf(\"%d: %d\\n\",i,*(aptr+i));\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>_Bool<\/em> pointer <code>aptr<\/code> is created at Line 6. Memory is allocated for the pointer at Line 10. Because the <em>sizeof<\/em> keyword returns 1 for a <em>_Bool<\/em>, 8 bytes are actually allocated. It would be cool if the compiler allocated 8 bits, but things don&#8217;t work that way.<\/p>\n<p>The <em>for<\/em> loop at Lines 18 through 22 fill the storage with ones. The <code>*(aptr+i)<\/code> notation is the pointer-equivalent of array notation, <code>a[i]<\/code>, which holds true for all C language variable types. Here&#8217;s the output:<\/p>\n<pre><code>0: 1\r\n1: 1\r\n2: 1\r\n3: 1\r\n4: 1\r\n5: 1\r\n6: 1\r\n7: 1<\/code><\/pre>\n<p>I suppose you could have all sorts of fun with <em>_Bool<\/em> variable types, including writing functions that accept or return <em>_Bool<\/em> values. I might even use such things in my code, though I&#8217;m still apt to either use a <em>char<\/em> or <em>short<\/em> variable type as a toggle or manipulate bits directly within an <em>int<\/em> variable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re wondering why the C overlords didn&#8217;t just call the <em>_Bool<\/em> variable type <em>bool<\/em>, they did! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2956\">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-2956","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\/2956","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=2956"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2956\/revisions"}],"predecessor-version":[{"id":2971,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2956\/revisions\/2971"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}